Last active
August 29, 2015 14:02
-
-
Save the-takeo/9933de6a3449599a92a1 to your computer and use it in GitHub Desktop.
素因数分解(PrimeDecomposition)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
namespace PrimeDecomposition | |
{ | |
public static class PrimeDecomposition | |
{ | |
public static List<int> DoPrimeDecomposition(int num) | |
{ | |
List<int> primaryFactors = new List<int>(); | |
//iを奇数だけとか、素数の必要条件で限定すればより効率がよくなる | |
for (int i = 2; i <= Math.Sqrt(num); i++) | |
{ | |
while (num % i == 0) | |
{ | |
primaryFactors.Add(i); | |
num = num / i; | |
} | |
} | |
primaryFactors.Add(num); | |
return primaryFactors; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment