prime-factoring

Prime factors in python

扶醉桌前 提交于 2019-11-26 22:10:16
问题 I am looking for prime factors of 2500 with the code below, but my code only prints 2 currently and I am unsure why this is the case. no = 2500 count = 0 # Finding factors of 2500 for i in range(1,no): if no%i == 0: # Now that the factors have been found, the prime factors will be determined for x in range(1,no): if i%x==0: count = count + 1 """Checking to see if the factor of 2500, itself only has two factor implying it is prime""" if count == 2: print i Thanks 回答1: using sieve of

Prime factorization - list

岁酱吖の 提交于 2019-11-26 18:50:44
I am trying to implement a function primeFac() that takes as input a positive integer n and returns a list containing all the numbers in the prime factorization of n . I have gotten this far but I think it would be better to use recursion here, not sure how to create a recursive code here, what would be the base case? to start with. My code: def primes(n): primfac = [] d = 2 while (n > 1): if n%d==0: primfac.append(d) # how do I continue from here... ? Daniel Fischer A simple trial division: def primes(n): primfac = [] d = 2 while d*d <= n: while (n % d) == 0: primfac.append(d) # supposing you

Fast prime factorization module

时间秒杀一切 提交于 2019-11-26 18:46:01
问题 I am looking for an implementation or clear algorithm for getting the prime factorization of N in either python, pseudocode or anything else well-readable. There are a few demands/facts: N is between 1 and ~20 digits No pre-calculated lookup table, memoization is fine though. Need not to be mathematically proven (e.g. could rely on the Goldbach conjecture if needed) Need not to be precise, is allowed to be probabilistic/deterministic if needed I need a fast prime factorization algorithm, not

Project Euler #3 takes forever in Java

被刻印的时光 ゝ 提交于 2019-11-26 18:37:51
问题 Problem #3 on Project Euler is: The prime factors of 13195 are 5, 7, 13 and 29. What is the largest prime factor of the number 600851475143? My solution takes forever. I think I got the right implementation; however, when testing with the big number, I have not being able to see the results. It runs forever. I wonder if there's something wrong with my algorithm: public class LargestPrimeFactor3 { public static void main(String[] args) { long start, end, totalTime; long num = 600851475143L;

Finding largest prime number out of 600851475143?

会有一股神秘感。 提交于 2019-11-26 15:32:59
I'm trying to solve problem 3 from http://projecteuler.net . However, when I run thing program nothing prints out. What am I doing wrong? Problem: What is the largest prime factor of the number 600851475143 ? public class project_3 { public boolean prime(long x) // if x is prime return true { boolean bool = false; for(long count=1L; count<x; count++) { if( x%count==0 ) { bool = false; break; } else { bool = true; } } return bool; } public static void main(String[] args) { long ultprime = 0L; // largest prime value project_3 object = new project_3(); for(long x=1L; x <= 600851475143L; x++) { if

Algorithm to find Largest prime factor of a number

本秂侑毒 提交于 2019-11-26 01:36:51
问题 What is the best approach to calculating the largest prime factor of a number? I\'m thinking the most efficient would be the following: Find lowest prime number that divides cleanly Check if result of division is prime If not, find next lowest Go to 2. I\'m basing this assumption on it being easier to calculate the small prime factors. Is this about right? What other approaches should I look into? Edit: I\'ve now realised that my approach is futile if there are more than 2 prime factors in

Segmented Sieve of Eratosthenes?

点点圈 提交于 2019-11-25 23:40:15
问题 It\'s easy enough to make a simple sieve: for (int i=2; i<=N; i++){ if (sieve[i]==0){ cout << i << \" is prime\" << endl; for (int j = i; j<=N; j+=i){ sieve[j]=1; } } cout << i << \" has \" << sieve[i] << \" distinct prime factors\\n\"; } But what about when N is very large and I can\'t hold that kind of array in memory? I\'ve looked up segmented sieve approaches and they seem to involve finding primes up until sqrt(N) but I don\'t understand how it works. What if N is very large (say 10^18)?