primes

Closest Prime Number in Python

旧巷老猫 提交于 2019-12-23 05:49:54
问题 I need a user to enter a number and enter out the closest prime number to the value they put in. I am struggling on how to check the prime numbers before and after the number they put in. The last part is to print the smaller value of the two prime numbers if they are the same distance away from the inputted number. n = int(input("Enter n: ")) holder1 = n holder2 = n prime = True holder3 = 0 holder4 = 0 for i in range(2,n): if (n % i) == 0: prime = False if(prime == True): print("The prime

C# Sieve of Eratosthenes

本秂侑毒 提交于 2019-12-23 03:45:15
问题 I have written this code to find prime numbers, and it works well, but the calculation speeds are incredibly slow..... Am I doing this wrong? I know that I might be really doing this the wrong way, but please help me! Thank you very much! using System; using System.Collections.Generic; namespace Primenumbers { class MainClass { public static void Main (string[] args) { List<int> NoPrime = new List<int>(); for(int x = 2; x < 10000;x++) { for(int y = x * 2;y < 10000;y = y + x) { if(!NoPrime

To find a number is prime, Why checking till n/2 is better. What is the reason for avoiding numbres in second half of n

故事扮演 提交于 2019-12-23 03:21:46
问题 To check if a number is prime or not, the naive way is to try dividing the number by 2 thru n, and if any operation gets remainder as 0, then we say the given number is not prime. But its optimal to divide and check only till n/2 (am aware much better way is till sqrt(n) ), I want to know the reason for skipping the second half. say if we need to check number 11 is prime or not, 11/2 = 5. if we do 11/6 or 11/7 or 11/8 or 11/9 or 11/10 in neither of these cases we get remainder as 0. So is the

Removing non-primes numbers from list

你。 提交于 2019-12-23 02:48:35
问题 I've got something like this: palindromes=[1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 101, 111, 121, ..., 99799, 99899, 99999] # of course im generating it :) def isPrime(number): for i in range(2,int(number/2)+1): if number%i == 0: return True return False def removeNonPrimes(palindromes): for palindrom in palindromes: if isPrime(palindrom): palindromes.remove(palindrom) return palindromes palindromes = removeNonPrimes(palindromes) And it doesnt remove all non primes I can not figure out why 回答1: In

Finding Primes Using Parallel

元气小坏坏 提交于 2019-12-22 10:23:29
问题 So I created the below method to find all the primes up to a certain number. Any suggestions as to how to speed it up? I call it like so; interval = (value + NOOFTHREADS - 1) / NOOFTHREADS; int max = interval * NOOFTHREADS; tickets = new List<int>(NOOFTHREADS); for (int i = 1; i <= NOOFTHREADS; i++) { tickets.Add(i * (max / NOOFTHREADS)); } Enumerable.Range(1, NOOFTHREADS) .AsParallel() .ForAll(_ => findPrimes()); With some global variables; static List<int> vals = new List<int>(); static

Modulus warning in R- Lehmann Primality Test

安稳与你 提交于 2019-12-22 10:17:31
问题 I spent a little time hacking an R implementation of the lehmann primality test. The function design I borrowed from http://davidkendal.net/articles/2011/12/lehmann-primality-test Here is my code: primeTest <- function(n, iter){ a <- sample(1:(n-1), 1) lehmannTest <- function(y, tries){ x <- ((y^((n-1)/2)) %% n) if (tries == 0) { return(TRUE) }else{ if ((x == 1) | (x == (-1 %% n))){ lehmannTest(sample(1:(n-1), 1), (tries-1)) }else{ return(FALSE) } } } lehmannTest(a, iter) } primeTest(4, 50) #

Decompose integers larger than 100 digits [closed]

╄→尐↘猪︶ㄣ 提交于 2019-12-22 09:06:00
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 5 years ago . X and Y are integers larger than 100 digits. Find the integer P which is within the range [ X , Y [ and that guaranties the "best" prime decomposition (i.e. the decomposition with the most unique prime factors). What I've done is just check the primality and decompose each number in the range and find the number

is this primes generator pythonic

牧云@^-^@ 提交于 2019-12-22 07:41:03
问题 Is the following code for generating primes pythonic? def get_primes(n): primes=[False,False]+[True]*(n-1) next_p=(i for i,j in enumerate(primes) if j) while True: p=next(next_p) yield p primes[p*p::p]=[False]*((n-p*p)//p+1) Note that next(next_p) will eventually throw a StopIteration error which somehow ends the function get_primes. Is that bad? Also note that next_p is a generator which iterates over primes, however primes changes during iteration. Is that bad style? adding the following if

is this primes generator pythonic

…衆ロ難τιáo~ 提交于 2019-12-22 07:40:51
问题 Is the following code for generating primes pythonic? def get_primes(n): primes=[False,False]+[True]*(n-1) next_p=(i for i,j in enumerate(primes) if j) while True: p=next(next_p) yield p primes[p*p::p]=[False]*((n-p*p)//p+1) Note that next(next_p) will eventually throw a StopIteration error which somehow ends the function get_primes. Is that bad? Also note that next_p is a generator which iterates over primes, however primes changes during iteration. Is that bad style? adding the following if

Is there an upper limit on .txt file size?

本秂侑毒 提交于 2019-12-22 01:37:54
问题 As a Christmas gift I have written a small program in Java to calculate primes. My intention was to leave it on all night, calculating the next prime and writing it to a .txt file. In the morning I would kill the program and take the .txt file to my friend for Christmas. Is there anything I should be worried about? Bear in mind that this is true beginner Ziggy you are talking to, not some smart error checking ASM guy. EDIT More specifically, since I will be leaving this program on all night