primes

Get list of primes to N

自古美人都是妖i 提交于 2021-02-20 19:13:09
问题 I'm trying to write a function which takes an Int and returns all of the prime numbers up to and including that Int. for example "list of primes for 8" = List(3,5,7) This is what I have so far : def isPrime(i: Int): Boolean = { if (i <= 1) false else if (i == 2) true else !(2 to (i - 1)).exists(x => i % x == 0) } //> isPrime: (i: Int)Boolean def getListOfPrimesToN(n : Int) = { } for the function getListOfPrimesToN I plan to 1. create a List "l" of size n and populate it with elements ranging

Big primes loop with GMP library C++

冷暖自知 提交于 2021-02-10 15:51:21
问题 It's the first time that I use the gmp library, so I'm really lost, I've found a code implementing the "miller rabin primality test" in c++ but I wanted to be able to apply it to integers with arbitrary precision so I installed the GMP library. The problem is, I've got no idea of how GMP library actually works (I've read trough a few pages of the manual but I understand very little about it also since I haven't even studied object oriented programming), I want to adapt the primality test to

Reducing memory usage when designing a sieve of eratosthenes in C

匆匆过客 提交于 2021-02-08 08:28:17
问题 I'm trying to design a sieve of eratosthenes in C but I've run into two strange problems which I can't figure out. Here's my basic program outline. Ask users to set a range to display primes from. If the range minimum is below 9, set the minimum as 9. Fill an array with all odd numbers in the range. 1) I'm trying to reduce memory usage by declaring variable size arrays like so: if (max<=UINT_MAX) unsigned int range[(max-min)/2]; else if (max<=ULONG_MAX) unsigned long int range[(max-min)/2];

Check if an int is prime Java

会有一股神秘感。 提交于 2021-02-05 07:00:36
问题 sorry for the "fix my code" post EDIT: related more to syntax of a for loop than prime numbers, also solved now. My task is to take an int from the console and print out (on separate lines) all the prime numbers from 1 to n inclusive. My method starts at n, checks if its prime, then increments n down by one and loops until n=2. To check if a number is prime I run a loop, checking is the remainder of diving the number by x is equal to zero, with x starting at 2 and stopping at root(n). Now

Sum of primes below 2,000,000 in python

跟風遠走 提交于 2021-02-04 14:08:41
问题 I am attempting problem 10 of Project Euler, which is the summation of all primes below 2,000,000. I have tried implementing the Sieve of Erasthotenes using Python, and the code I wrote works perfectly for numbers below 10,000. However, when I attempt to find the summation of primes for bigger numbers, the code takes too long to run (finding the sum of primes up to 100,000 took 315 seconds). The algorithm clearly needs optimization. Yes, I have looked at other posts on this website, like

Sum of primes below 2,000,000 in python

烂漫一生 提交于 2021-02-04 14:08:24
问题 I am attempting problem 10 of Project Euler, which is the summation of all primes below 2,000,000. I have tried implementing the Sieve of Erasthotenes using Python, and the code I wrote works perfectly for numbers below 10,000. However, when I attempt to find the summation of primes for bigger numbers, the code takes too long to run (finding the sum of primes up to 100,000 took 315 seconds). The algorithm clearly needs optimization. Yes, I have looked at other posts on this website, like

Sum of primes below 2,000,000 in python

╄→гoц情女王★ 提交于 2021-02-04 14:08:12
问题 I am attempting problem 10 of Project Euler, which is the summation of all primes below 2,000,000. I have tried implementing the Sieve of Erasthotenes using Python, and the code I wrote works perfectly for numbers below 10,000. However, when I attempt to find the summation of primes for bigger numbers, the code takes too long to run (finding the sum of primes up to 100,000 took 315 seconds). The algorithm clearly needs optimization. Yes, I have looked at other posts on this website, like

Finding all the 10-digit prime numbers, that have seven “7” in a row - Python

梦想的初衷 提交于 2021-01-27 20:25:27
问题 so as it states in the title, I'm trying to generate a list of all the 10-digit prime numbers, that have 7x7 in a row. More precisely, I mean the numbers that can be written as follows: xxx7777777, xx7777777x, x7777777xx, 7777777xxx. My idea is to generate a list of all this numbers, and then check which one of them is prime. The code is as follows: import time def GeneratingTable(): A = [] for i in range (1,10): for j in range (0,10): for k in range (0,10): A.append(i*1000000000+j*100000000

Fastest way of testing if a number is prime with Python [duplicate]

心已入冬 提交于 2021-01-01 13:29:26
问题 This question already has answers here : Fast prime factorization module (7 answers) Closed 3 years ago . I'm trying to get a fast way to determine if a number is prime using Python. I have two functions to do this. Both return either True or False. Function isPrime1 is very fast to return False is a number is not a prime. For example with a big number. But it is slow in testing True for big prime numbers. Function isPrime2 is faster in returning True for prime numbers. But if a number is big

Fastest way of testing if a number is prime with Python [duplicate]

孤街醉人 提交于 2021-01-01 13:27:43
问题 This question already has answers here : Fast prime factorization module (7 answers) Closed 3 years ago . I'm trying to get a fast way to determine if a number is prime using Python. I have two functions to do this. Both return either True or False. Function isPrime1 is very fast to return False is a number is not a prime. For example with a big number. But it is slow in testing True for big prime numbers. Function isPrime2 is faster in returning True for prime numbers. But if a number is big