primes

Finding the Nth Twin Prime

a 夏天 提交于 2019-12-09 04:57:14
问题 I was trying to solve a problem on SPOJ. We are required to calculate the nth twin prime pair( primes differing by 2). n can be as large as 10^5. I tried a precalculation using a sieve, I had to sieve up to 10^8 to get the maximum n twin prime, but the time limit is strict(2s) and it times out. I noticed people have solved it in 0.00 seconds, so i looked around for a formula on google, and couldnt get anything helpful. Could someone please guide me? Thanks in advance!! 回答1: So basically,

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-09 03:58:29
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 case for any given number n. Is the reason for avoiding second half this? "if you divide the given

Java Program for Prime numbers

左心房为你撑大大i 提交于 2019-12-09 02:21:57
问题 Problem In this project you will write a Java program that reads a positive integer n from standard input, then prints out the first n prime numbers. We say that an integer m is divisible by a non-zero integer d if there exists an integer k such that m = k d , i.e. if d divides evenly into m. Equivalently, m is divisible by d if the remainder of m upon (integer) division by d is zero. We would also express this by saying that d is a divisor of m. A positive integer p is called prime if its

Fast multiplication and subtraction modulo a prime

梦想与她 提交于 2019-12-08 16:04:44
问题 I need to optimize some code where I multiply a vector of ints (32 bit) by a scalar modulo p (where p is the prime number (2^32)-5) and then subtract that vector from another vector modulo p. The code looks like this: public static void multiplyAndSubtract(long fragmentCoefficient, long[] equationToSubtractFrom, long[] equationToSubtract) { for (int i = 0; i < equationToSubtractFrom.length; i++) { equationToSubtractFrom[i] = modP(equationToSubtractFrom[i] - multiplyModP(fragmentCoefficient,

Determining if a given number is a prime in haskell

℡╲_俬逩灬. 提交于 2019-12-08 15:22:52
问题 So I have devised the following function for seeing if a given number is a prime in Haskell (it assumes the first prime is 2): isPrime k = length [ x | x <- [2..k], k `mod` x == 0] == 1 it has the obvious pitfall of continuing the evaluation even if it is divisible by several numbers :(. Is there any sane way of "cutting" the evaluation when it finds more than one solution, using list comprehensions? Also, which other implementations would you you try on? I'm not looking for performance here,

Next Prime number Java only working with certain numbers

南笙酒味 提交于 2019-12-08 12:44:03
问题 This function its only working for certain numbers, but for 15, or 5 it does not give me correct next prime. public static int nextPrime(int n) { boolean isPrime = false; int m = (int) Math.ceil(Math.sqrt(n)); int start = 3; if (n % 2 == 0) { n = n + 1; } while (!isPrime) { isPrime = true; for (int i = start; i <= m; i = i + 2) { if (n % i == 0) { isPrime = false; break; } } if (!isPrime) { n = n + 2; } } return n; } 回答1: You don't need to go upto sqrt(n), you need to go upto sqrt(number)

Iterate all coprime pairs using constant space?

人盡茶涼 提交于 2019-12-08 12:02:18
问题 I can generate all coprime pairs by following the ternary-tree algorithm listed on wikipedia: https://en.wikipedia.org/wiki/Coprime_integers Quickly: Start with two coprime branches: (2,1), (3,1), then iterate: Branch 1: (2m-n,m) Branch 2: (2m+n,m) Branch 3: (m+2n,n) However the space used will grow by a factor of three for each pair produced (and say printed, or otherwise not kept in memory). Here might be a solution in haskell: Generating sorted list of all possible coprimes But I was

Computing prime numbers up to N integers

吃可爱长大的小学妹 提交于 2019-12-08 07:56:00
问题 I am trying to write a little script myself to compute all of the prime numbers up to n (a user submitted argument) and would appreciate a little bit of help. I want to use ArrayLists to write this function, and hopefully make it as efficient as possible - another big thing for me that I can't seem to grasp is doing everything as is standard and good practice (i.e having classes in capital letters, etc) so if you wouldn't mind please point out any mistakes in that regard so I can fix them.

C program to find a prime number

☆樱花仙子☆ 提交于 2019-12-08 07:16:52
问题 I wrote a C program which tells whether a given number is prime or not. But it has a problem in it. It is working fine for numbers other than multiples of 5. But it is showing the multiples of 5 as prime like 15, 25, 35, 45... . I am not able to find the error. I've tried comparing it with other programs on the internet but I am not able to find the error. #include <stdio.h> int primeornot(int a) { int i; for (i = 2; i <= a / 2; i++) { if (a % i == 0) { return 0; break; } else { return 1; } }

python sum of primes

断了今生、忘了曾经 提交于 2019-12-08 05:54:03
问题 I am tying to make a python program that will generate the sum of primes for a number, but the program is not giving the correct result,please tell me why. b=1 #generates a list of numbers. while b<100: b=b+1 x = 0.0 a = 0 d = 0 #generates a list of numbers less than b. while x<b: x=x+1 #this will check for divisors. if (b/x)-int(b/x) == 0.0: a=a+1 if a==2: #if it finds a prime it will add it. d=d+b print d I made it generate a list of primes successfully, but i could not get the primes to