primality-test

Calculating whether number is prime in Prolog

☆樱花仙子☆ 提交于 2019-12-11 03:38:47
问题 I am trying to calculate if the input is a prime number but something goes wrong... here's my code: primeNumber(X):- prime_prime(A, 1). prime_prime(A, B):- R is A mod B, R =:= 1, R =:= A. prime_prime(X, B):- B < A, Next is B + 1, prime_prime(A, Next). It gives me false every time. Anyone got any clues or idea on what I am doing wrong? 回答1: See http://www.swi-prolog.org/pldoc/man?function=mod/2: +IntExpr1 mod +IntExpr2 Modulo, defined as Result = IntExpr1 - (IntExpr1 div IntExpr2) × IntExpr2,

MillerRabin primality test in C#

久未见 提交于 2019-12-10 10:54:42
问题 Welcome. I am trying to implement MillerRabin test for checking if large given number is a prime. Here is my code: public static bool MillerRabinTest(BigInteger number) { BigInteger d; var n = number - 1; var s = FindK(n, out d); BigInteger a = 2; BigInteger y = Calc(a, d, number); //a^d mod number if (y != BigInteger.One && y != n) { for (var r = 1; r <= s - 1; r++) { y = Calc(y, 2, number); if (y == 1) return false; } if (y != n) return false; } return true; //it is probably prime } It is

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,

MillerRabin primality test in C#

我的未来我决定 提交于 2019-12-06 08:47:42
Welcome. I am trying to implement MillerRabin test for checking if large given number is a prime. Here is my code: public static bool MillerRabinTest(BigInteger number) { BigInteger d; var n = number - 1; var s = FindK(n, out d); BigInteger a = 2; BigInteger y = Calc(a, d, number); //a^d mod number if (y != BigInteger.One && y != n) { for (var r = 1; r <= s - 1; r++) { y = Calc(y, 2, number); if (y == 1) return false; } if (y != n) return false; } return true; //it is probably prime } It is working fine for small Bigintegers. But if my programs needs to evalute numbers containing of more than

Learning Haskell: Seemingly Circular Program - Please help explain

为君一笑 提交于 2019-12-05 18:23:39
问题 I'm currently going through the book "The Haskell Road to Logic, Math, and Programming" by Doets and Van Eijck. I've never been exposed to any functional programming language until this book, so keep that in mind. Still early in the book, it gives the following code for a primality test: ldp :: Integer -> Integer ldp n = ldpf primes1 n ldpf :: [Integer] -> Integer -> Integer ldpf (p:ps) n | rem n p == 0 = p | p^2 > n = n | otherwise = ldpf ps n primes1 :: [Integer] primes1 = 2 : filter prime

Check for a prime number using recursive helper function

牧云@^-^@ 提交于 2019-12-02 00:25:22
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) #f ) What would be a good approach using a recursive helper function?? Thanks! Using a helper simply

Miller Rabin Primality test accuracy

馋奶兔 提交于 2019-11-30 10:27:31
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)? 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 reported to be prime. We can bound the probability of this error using the formula on Wikipedia : If you

Time complexity of this primality testing algorithm?

点点圈 提交于 2019-11-29 16:45:13
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? Think about the worst-case runtime of this function, which happens if the number is indeed prime. In that case, the inner loop will execute as many times as possible. Since each iteration of the loop does a constant amount of

Program that checks if a number is prime number

痞子三分冷 提交于 2019-11-27 16:32:11
Hello I have created this program to check if a number is a prime number. It works but for some reason says that 999 is a prime number. Where is my mistake. It would be great if someone explained. Thank You! Here is my program: number = raw_input('Enter a Number: ') nnumber = int(number) prime_range = range(2, nnumber) for x in prime_range: if nnumber % x == 0: print 'Not a Prime Number!' break else: print 'Prime Number!' break Trace it. x starts with 2 , then tests 999 % 2 ; it is 1 , so else is executed, "Prime number!" is printed, and loop is broken out of. Program ends. Instead, you need

Check if number is prime number

给你一囗甜甜゛ 提交于 2019-11-26 18:45:45
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 prime number"); Console.ReadLine(); } Soner Gönül var number; Console.WriteLine("Accept number:"); number