primality-test

Time complexity of this primality testing algorithm?

北城余情 提交于 2020-04-05 06:35:26
问题 I have the following code which determines whether a number is prime: public static boolean isPrime(int n){ boolean answer = (n>1)? true: false; for(int i = 2; i*i <= n; ++i) { System.out.printf("%d\n", i); if(n%i == 0) { answer = false; break; } } return answer; } How can I determine the big-O time complexity of this function? What is the size of the input in this case? 回答1: Think about the worst-case runtime of this function, which happens if the number is indeed prime. In that case, the

Check if number is prime number

混江龙づ霸主 提交于 2019-12-27 12:01:10
问题 I would just like to ask if this is a correct way of checking if number is prime or not? because I read that 0 and 1 are NOT a prime number. int num1; Console.WriteLine("Accept number:"); num1 = Convert.ToInt32(Console.ReadLine()); if (num1 == 0 || num1 == 1) { Console.WriteLine(num1 + " is not prime number"); Console.ReadLine(); } else { for (int a = 2; a <= num1 / 2; a++) { if (num1 % a == 0) { Console.WriteLine(num1 + " is not prime number"); return; } } Console.WriteLine(num1 + " is a

Check if number is prime number

好久不见. 提交于 2019-12-27 11:58:18
问题 I would just like to ask if this is a correct way of checking if number is prime or not? because I read that 0 and 1 are NOT a prime number. int num1; Console.WriteLine("Accept number:"); num1 = Convert.ToInt32(Console.ReadLine()); if (num1 == 0 || num1 == 1) { Console.WriteLine(num1 + " is not prime number"); Console.ReadLine(); } else { for (int a = 2; a <= num1 / 2; a++) { if (num1 % a == 0) { Console.WriteLine(num1 + " is not prime number"); return; } } Console.WriteLine(num1 + " is a

Generating large prime numbers in python

对着背影说爱祢 提交于 2019-12-22 11:10:06
问题 I can't seem to make random prime numbers using this code, please can someone help me? def RandomPrime(): prime = False while prime == False: n = random.randint(10000, 100000) if n % 2 != 0: for x in range(3, int(n**0.5), 2): if n % x ==0: prime = False else: prime = True return n 回答1: Imagine what happens if the last number in range(3, int(n**0.5), 2) is not an integer divisor of n : if n % x ==0: prime = False # not this else: prime = True # this So even if all previous checks evaluated

Check for a prime number using recursive helper function

好久不见. 提交于 2019-12-20 02:56:14
问题 I am trying to check if a number is prime using recursion. I was required to use a recursive helper function, but I am not sure how I should implement it. I think I know the algorithm, but I've never tried to use a recursive helper function in Racket. This is my current thoughts: See if n is divisible by i = 2 Set i = i + 1 If i^2 <= n continue. If no values of i evenly divided n , then it must be prime. This is what I have so far... (define (is_prime n) (if (<= n 1) #f (if (= (modulo n 2) 0)

Miller Rabin Primality test accuracy

一世执手 提交于 2019-12-18 13:36:18
问题 I know the Miller–Rabin primality test is probabilistic. However I want to use it for a programming task that leaves no room for error. Can we assume that it is correct with very high probability if the input numbers are 64-bit integers (i.e. long long in C)? 回答1: Miller–Rabin is indeed probabilistic, but you can trade accuracy for computation time arbitrarily. If the number you test is prime, it will always give the correct answer. The problematic case is when a number is composite, but is

Goldbach’s Conjecture in prolog

Deadly 提交于 2019-12-14 03:34:29
问题 Goldbach’s Conjecture : Every positive even number greater than 2 is the sum of two prime numbers. Eg 28 (5,23 and 11,17) I want Prolog code to print below (all combinations) : ?- goldbach(28, L). Output : L = [5,23]; L = [11, 17]; I have a code which prints single combination[5,23], but not the next [11,17]. is_prime(2). is_prime(3). is_prime(P) :- integer(P), P > 3, P mod 2 =\= 0, \+ has_factor(P,3). has_factor(N,L) :- N mod L =:= 0. has_factor(N,L) :- L * L < N, L2 is L + 2, has_factor(N

Prime number Logic, n/2 condition in a loop

徘徊边缘 提交于 2019-12-13 11:30:47
问题 The following code is for prime number. I want to know why we use i<=n/2 condition in the loop. C Program: #include <stdio.h> int main() { int n, i, flag = 0; printf("Enter a positive integer: "); scanf("%d",&n); for(i=2; i<=n/2; ++i) { // condition for nonprime number if(n%i==0) { flag=1; break; } } if (flag==0) printf("%d is a prime number.",n); else printf("%d is not a prime number.",n); return 0; } 回答1: Although this is C program. But prime number logic will be same for C and Java both

Determining if a number is prime in the above code [duplicate]

拟墨画扇 提交于 2019-12-11 18:19:05
问题 This question already has answers here : Program that checks if a number is prime number (5 answers) Prime numbers list generator immediately closing when run (2 answers) How come in this code to find prime numbers, is_prime(9) returns True? [duplicate] (2 answers) Closed 10 months ago . I have a simple problem: take a number and determine if it is prime. I cannot figure out why on earth the following does not work. def prime(x): for i in range (2, x): if x % i == 0: return False return True

What witnesses do i need for Rabin-Miller test for numbers up to 10¹⁸?

女生的网名这么多〃 提交于 2019-12-11 06:58:57
问题 What set of witnesses is sufficient for the Miller-Rabin test to be correct for all numbers up to 10¹⁸? I know that use of primes up to 17 as witnesses suffices for n < 341550071728321. 回答1: According to this record page, the set of 7 SPRP bases: {2, 325, 9375, 28178, 450775, 9780504, 1795265022} is sufficient for a deterministic test to at least n = 2^64 ( > 10^19) . 回答2: According to OEIS, use of witnesses up to 23 suffices for numbers up to 3825123056546413051 回答3: If you're willing to use