prime-factoring

Largest prime factor program takes aaaages - Java

扶醉桌前 提交于 2019-11-29 08:02:53
So this is problem 3 from project Euler. For those who don't know, I have to find out the largest prime factor of 600851475143. I have the below code: import java.lang.Math; // 600851475143 public class LargestPrimeFactor { public static void main(String[] stuff) { long num = getLong("What number do you want to analyse? "); long[] primes = primeGenerator(num); long result = 0; for(int i = 0; i < primes.length; i++) { boolean modulo2 = num % primes[i] == 0; if(modulo2) { result = primes[i]; } } System.out.println(result); } public static long[] primeGenerator(long limit) { int aindex = 0; long[

Finding prime factors

泄露秘密 提交于 2019-11-29 03:55:51
问题 #include <iostream> using namespace std; void whosprime(long long x) { bool imPrime = true; for(int i = 1; i <= x; i++) { for(int z = 2; z <= x; z++) { if((i != z) && (i%z == 0)) { imPrime = false; break; } } if(imPrime && x%i == 0) cout << i << endl; imPrime = true; } } int main() { long long r = 600851475143LL; whosprime(r); } I'm trying to find the prime factors of the number 600851475143 specified by Problem 3 on Project Euler (it asks for the highest prime factor, but I want to find all

Racket Programming. Where am I going wrong?

扶醉桌前 提交于 2019-11-28 14:29:16
The question i'm trying to answer: The prime factors of 13195 are 5, 7, 13 and 29. What is the largest prime factor of the number 600851475143 ? Where am I going wrong? my prime? test seems to be the issue, but it works fine on relatively small numbers. However the prime? test gives a wrong answer with larger numbers. Is there an easier way to go about this? (define b 3) (define z 0) (define divides? (lambda (a b) (= (remainder a b) 0))) (define (prime? n) (cond ((or (= n 1) (= n 0)) false) ((even? n) false) ((= n 2) true) ((= n b) true) ((divides? n b) false) (else (and (set! b (+ b 1))

Efficiently finding all divisors of a number

我的未来我决定 提交于 2019-11-28 07:44:04
So I simply want to find all the divisors of a given number (excepting the number itself). Currently, I have this: public static List<int> proper_divisors(int x) { List<int> toreturn = new List<int>(); toreturn.Add(1); int i = 0; int j=1; int z = 0; while (primes.ElementAt(i) < Math.Sqrt(x)) { if (x % primes.ElementAt(i) == 0) { toreturn.Add(primes.ElementAt(i)); toreturn.Add(x / primes.ElementAt(i)); j = 2; z = (int)Math.Pow(primes.ElementAt(i), 2); while (z < x) { if (x % z == 0) { toreturn.Add(z); toreturn.Add(x / z); j++; z = (int)Math.Pow(primes.ElementAt(i), j); } else { z = x; } } } i++

Brute-force, single-threaded prime factorization

喜欢而已 提交于 2019-11-28 04:11:52
Up for consideration is the following function which can be used to (relatively quickly) factor a 64-bit unsigned integer into its prime factors. Note that the factoring is not probabalistic (i.e., it is exact). The algorithm is already fast enough to find that a number is prime or has few very large factors in a matter of several seconds, on modern hardware. The question: Can any improvements be made to the algorithm presented, while keeping it single-threaded, so that it can factor (arbitrary) very large unsigned 64-bit integers faster, preferably without using a probabalistic approach (e.g.

Fast prime factorization module

荒凉一梦 提交于 2019-11-28 02:46:17
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 only for itself, but for usage in many other algorithms like calculating the Euler phi(n) . I have

Prime factors in python

本小妞迷上赌 提交于 2019-11-28 02:12:20
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 using sieve of eratosthenes to first generate list of primes: from math import sqrt def sieve_of_eratosthenes(n): primes = range(3, n

Project Euler #3 takes forever in Java

独自空忆成欢 提交于 2019-11-27 16:16:07
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; long pFactor = 0; start = System.currentTimeMillis(); for(int i = 2; i < num; i++) { if(isPrime(i)) { if

Prime Factors In C#

一笑奈何 提交于 2019-11-27 14:06:54
问题 I want to create a program in C# 2005 which calculates prime factors of a given input. i want to use the basic and simplest things, no need to create a method for it nor array things etc. just simple modulus. is there any code which fulfills what i desire? here is the code for finding simple factors, i need this code to be modified to calculate prime factors class Program { static void Main(string[] args) { int a, b; Console.WriteLine("Please enter your integer: "); a = int.Parse(Console

Efficiently finding all divisors of a number

六眼飞鱼酱① 提交于 2019-11-27 01:57:23
问题 So I simply want to find all the divisors of a given number (excepting the number itself). Currently, I have this: public static List<int> proper_divisors(int x) { List<int> toreturn = new List<int>(); toreturn.Add(1); int i = 0; int j=1; int z = 0; while (primes.ElementAt(i) < Math.Sqrt(x)) { if (x % primes.ElementAt(i) == 0) { toreturn.Add(primes.ElementAt(i)); toreturn.Add(x / primes.ElementAt(i)); j = 2; z = (int)Math.Pow(primes.ElementAt(i), 2); while (z < x) { if (x % z == 0) { toreturn