Last active
December 15, 2015 02:49
-
-
Save jasonsperske/5189806 to your computer and use it in GitHub Desktop.
A Python port of this:http://www.vogella.com/articles/JavaAlgorithmsPrimeFactorization/article.html
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
import java.util.ArrayList; | |
import java.util.List; | |
public class PrimeFactorsOf { | |
public static void main(String ... args) { | |
System.out.println(primeFactorsOf(19252389595L)); | |
} | |
static List<Long> primeFactorsOf(long val) { | |
long n = val; | |
List<Long> factors = new ArrayList<Long>(); | |
for (long i = 2; i <= n / i; i++) { | |
while (n % i == 0) { | |
factors.add(i); | |
n /= i; | |
} | |
} | |
if (n > 1) { | |
factors.add(n); | |
} | |
return factors; | |
} | |
} |
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
def primeFactorsOf(val): | |
n = val | |
factors = [] | |
i = 2 | |
while i <= (n / i): | |
while n % i == 0: | |
factors.append(i) | |
n /= i | |
i += 1 | |
if n > 1: | |
factors.append(n) | |
return factors | |
if __name__=="__main__": | |
print primeFactorsOf(19252389595) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment