Skip to content

Instantly share code, notes, and snippets.

@the-takeo
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save the-takeo/9933de6a3449599a92a1 to your computer and use it in GitHub Desktop.
Save the-takeo/9933de6a3449599a92a1 to your computer and use it in GitHub Desktop.
素因数分解(PrimeDecomposition)
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