primes

Fast algorithm for finding prime numbers? [duplicate]

拥有回忆 提交于 2019-12-30 06:26:12
问题 This question already has answers here : Which is the fastest algorithm to find prime numbers? (14 answers) Closed 6 years ago . First of all - I checked a lot in this forum and I haven't found something fast enough . I try to make a function that returns me the prime numbers in a specified range. For example I did this function (in C#) using the sieve of Eratosthenes. I tried also Atkin's sieve but the Eratosthenes one runs faster (in my implementation): public static void SetPrimesSieve(int

Fast algorithm for finding prime numbers? [duplicate]

孤街浪徒 提交于 2019-12-30 06:25:31
问题 This question already has answers here : Which is the fastest algorithm to find prime numbers? (14 answers) Closed 6 years ago . First of all - I checked a lot in this forum and I haven't found something fast enough . I try to make a function that returns me the prime numbers in a specified range. For example I did this function (in C#) using the sieve of Eratosthenes. I tried also Atkin's sieve but the Eratosthenes one runs faster (in my implementation): public static void SetPrimesSieve(int

What's the ideal implementation for the Sieve of Eratosthenes between Lists, Arrays, and Mutable Arrays?

社会主义新天地 提交于 2019-12-30 04:45:18
问题 In Haskell, I've found three simple implementations of the Sieve of Eratosthenes on the Rosetta Code page. Now my question is, which one should be used in which situations? Correcting my initial reasoning would be helpful too: I'm assuming the List one is the most idiomatic and easy to read for a Haskeller. Is it correct, though? I'm wondering if it suffers from the same problems as another list-based sieve that I then learned was not actually implementing the algorithm: (edit: shown here is

Find out 20th, 30th, nth prime number. (I'm getting 20th but not 30th?) [Python]

穿精又带淫゛_ 提交于 2019-12-30 03:23:05
问题 The question is to find the 1000th prime number. I wrote the following python code for this. The problem is, I get the right answer for the 10th , 20th prime but after that each increment of 10 leaves me one off the mark. I can't catch the bug here :( count=1 #to keep count of prime numbers primes=() #tuple to hold primes candidate=3 #variable to test for primes while count<20: for x in range(2,candidate): if candidate%x==0: candidate=candidate+2 else : pass primes=primes+(candidate,)

python prime numbers Sieve of Eratosthenes

我的梦境 提交于 2019-12-30 00:42:05
问题 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

Stuck on Project Euler #3 in python

一曲冷凌霜 提交于 2019-12-29 08:25:33
问题 The prime factors of 13195 are 5, 7, 13 and 29. What is the largest prime factor of the number 600851475143 ? Ok, so i am working on project euler problem 3 in python. I am kind of confused. I can't tell if the answers that i am getting with this program are correct or not. If somone could please tell me what im doing wrong it would be great! #import pdb odd_list=[] prime_list=[2] #Begin with zero so that we can pop later without errors. #Define a function that finds all the odd numbers in

Find a largest prime number less than n [closed]

早过忘川 提交于 2019-12-29 08:25:11
问题 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 6 years ago . How can I find a largest prime number which is less than n, where n ≤ 10¹⁸ ? Please help me find an Efficient Algorithm. for(j=n;j>=2;j--) { if(j%2 == 1) { double m = double(j); double a = (m-1)/6.0; double b =

Sieve of Eratosthenes Scheme

岁酱吖の 提交于 2019-12-29 08:05:42
问题 I've been searching the web for an implementation of the Sieve of Eratosthenes in scheme and although I came up with a lot of content, none of them seemed to have made it like I need it to be done. The problem is most algorithms either use a static end or use iteration. This paired with my lack of knowledge of the language led me to ask all of you for help. I need an implementation of the Sieve that takes in one argument (number to Sieve until), uses only recursion and has a list of "cons" of

Prolog Program To Check If A Number Is Prime

岁酱吖の 提交于 2019-12-29 07:07:29
问题 I wrote the following program based on the logic that a prime number is only divisible by 1 and itself. So I just go through the process of dividing it to all numbers that are greater than one and less than itself, but I seem to have a problem since I get all entered numbers as true. Here's my code... divisible(X,Y) :- Y < X, X mod Y is 0, Y1 is Y+1, divisible(X,Y1). isprime(X) :- integer(X), X > 1, \+ divisible(X,2). Thanks in advance :) 回答1: I'm a beginner in Prolog but managed to fix your

Prime Number Formula

人走茶凉 提交于 2019-12-28 06:27:48
问题 I am trying to write a prime number function in C# and I am wondering if the follow code will work. It "appears" to work with the first 50 numbers or so. I just want to make sure it will work no matter how big the number is: static bool IsPrime(int number) { if ((number == 2) || (number == 3) || (number == 5) || (number == 7) || (number == 9)) return true; if ((number % 2 != 0) && (number % 3 != 0) && (number % 5 != 0) && (number % 7 != 0) && (number % 9 != 0) && (number % 4 != 0) && (number