primes

How can I convert an absolutely massive number to a string in a reasonable amount of time?

那年仲夏 提交于 2019-11-30 05:59:32
This is quite an odd problem I know, but I'm trying to get a copy of the current largest prime number in a file. Getting the number in integer form is fairly easy. I just run this. prime = 2**74207281 - 1 It takes about half a second and it works just fine. Operations are fairly quick as well. Dividing it by 10 (without decimals) to shift the digits is quick. However, str(prime) is taking a very long time. I reimplemented str like this, and found it was processing about a hundred or so digits per second. while prime > 0: strprime += str(prime%10) prime //= 10 Is there a way to do this more

Finding prime factors

陌路散爱 提交于 2019-11-30 05:26:58
#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 of them). However, when I try to run this program I don't get any results. Does it have to do with how

What is wrong with my isPrime method?

谁都会走 提交于 2019-11-30 05:06:44
This is my isPrime method: private static boolean isPrime(int num) { if (num % 2 == 0) return false; for (int i = 3; i * i < num; i += 2) if (num % i == 0) return false; return true; } I put isPrime(9) and it returns true . What is wrong with the method? Tareq Salah Your condition should be i * i <= num private static boolean isPrime(int num) { if (num == 2) return true; if (num < 2 || num % 2 == 0) return false; for (int i = 3; i * i <= num; i += 2) if (num % i == 0) return false; return true; } You didn't take number 9 in your consideration so 9<9 will result false. But you need to check 9.

python prime numbers Sieve of Eratosthenes

怎甘沉沦 提交于 2019-11-30 04:07:25
Hi can anyone tell me how to implement Sieve of Eratosthenes within this code to make it fast? Help will be really appreciated if you can complete it with sieve. I am really having trouble doing this in this particular code. #!/usr/bin/env python import sys T=10 #no of test cases t=open(sys.argv[1],'r').readlines() import math def is_prime(n): if n == 2: return True if n%2 == 0 or n <= 1: return False sqr = int(math.sqrt(n)) + 1 for divisor in range(3, sqr, 2): if n%divisor == 0: return False return True #first line of each test case a=[1,4,7,10,13,16,19,22,25,28] count=0 for i in a: b=t[i]

Sample code for fast primality testing in C# [duplicate]

人盡茶涼 提交于 2019-11-30 03:01:20
Possible Duplicate: Fastest algorithm for primality test Would appreciate a reference to sample code for fast primality testing in C#, preferably using BigInteger or other variable size type. This is a Miller Rabin test in c#: bool MillerRabin(ulong n) { ulong[] ar; if (n < 4759123141) ar = new ulong[] { 2, 7, 61 }; else if (n < 341550071728321) ar = new ulong[] { 2, 3, 5, 7, 11, 13, 17 }; else ar = new ulong[] { 2, 3, 5, 7, 11, 13, 17, 19, 23 }; ulong d = n - 1; int s = 0; while ((d & 1) == 0) { d >>= 1; s++; } int i, j; for (i = 0; i < ar.Length; i++) { ulong a = Math.Min(n - 2, ar[i]);

Why is this prime test so slow?

孤街浪徒 提交于 2019-11-30 02:40:22
问题 This code was taken from the book "Haskell Road to Logic, Math and Programming". It implements sieve of eratosthenes algorithm and solves Project Euler Problem 10. sieve :: [Integer] -> [Integer] sieve (0 : xs) = sieve xs sieve (n : xs) = n : sieve (mark xs 1 n) where mark :: [Integer] -> Integer -> Integer -> [Integer] mark (y:ys) k m | k == m = 0 : (mark ys 1 m) | otherwise = y : (mark ys (k+1) m) primes :: [Integer] primes = sieve [2..] -- Project Euler #10 main = print $ sum $ takeWhile (

Sieve of Atkin explanation

耗尽温柔 提交于 2019-11-30 01:45:36
I am doing a project at the moment and I need an efficient method for calculating prime numbers. I have used the sieve of Eratosthenes but, I have been searching around and have found that the sieve of Atkin is a more efficient method. I have found it difficult to find an explanation (that I have been able to understand!) of this method. How does it work? Example code (preferably in C or python) would be brilliant. Edit: thanks for your help, the only thing that I still do not understand is what the x and y variables are referring to in the pseudo code. Could someone please shed some light on

Calculating prime numbers in Scala: how does this code work?

一个人想着一个人 提交于 2019-11-29 22:38:43
So I've spent hours trying to work out exactly how this code produces prime numbers. lazy val ps: Stream[Int] = 2 #:: Stream.from(3).filter(i => ps.takeWhile{j => j * j <= i}.forall{ k => i % k > 0}); I've used a number of printlns etc, but nothings making it clearer. This is what I think the code does: /** * [2,3] * * takeWhile 2*2 <= 3 * takeWhile 2*2 <= 4 found match * (4 % [2,3] > 1) return false. * takeWhile 2*2 <= 5 found match * (5 % [2,3] > 1) return true * Add 5 to the list * takeWhile 2*2 <= 6 found match * (6 % [2,3,5] > 1) return false * takeWhile 2*2 <= 7 * (7 % [2,3,5] > 1)

Algorithm to find Lucky Numbers

不羁的心 提交于 2019-11-29 19:47:26
I came across this question.A number is called lucky if the sum of its digits, as well as the sum of the squares of its digits is a prime number. How many numbers between A and B are lucky? 1 <= A <= B <= 10 18 . I tried this. First I generated all possible primes between 1 and the number that could be resulted by summing the squares (81 *18 = 1458). I read in A and B find out maximum number that could be generated by summing the digits If B is a 2 digit number ( the max number is 18 generated by 99). For each prime number between 1 an max number. I applied integer partition algorithm. For

Algorithm to find all primes from 2 to 1000 not working

牧云@^-^@ 提交于 2019-11-29 18:30:16
问题 Here's a piece of code to compute all primes from 2 to 1000 using the statement, that a number n is a prime number iff: In the first version I think that I implemented the algorithm correctly: public class Giuga { public static void main(String[] args){ int n = 2; while(n<=1000){ int k = 1; long sum = 0; while(k<=n-1){ sum = sum+(long)Math.pow((double)k,(double)n-1); k++; } if(sum%n==n-1){ System.out.println(n + " is a prime."); } n++; } } } But, since the variable sum grows rapidly, an