Questions tagged [primality-test]

A primality test is an algorithm for determining whether an input number is prime.

A primality test is an algorithm for determining whether an input number is prime.

See https://en.wikipedia.org/wiki/Primality_test.

115 questions
1
vote
3 answers

Determine a prime number with right fold

I found this solution on the internet and I need some help understanding it: isPrime' :: Integer -> Bool isPrime' n = foldr (\x acc -> (n `rem` x) /= 0 && acc) True primes where primes = 2 : filter isPrime' [3,5..] A couple of things: My…
dopatraman
  • 13,416
  • 29
  • 90
  • 154
1
vote
3 answers

Trial Division faster than Sieve for Primality Test?

I wrote two primality tests in python. First one is based on trial division, the second one applies sieve of Eratosthenes. My understanding is that sieve should have a smaller time complexity than trial, so sieve should be asymptotically faster.…
1
vote
3 answers

Odd math error in Java

I'm writing a program to demonstrate Miller-Rabin probability testing in Java. The code is pretty much done... import java.util.Random; import java.util.Scanner; /** * Program to demonstrate Miller-Rabin primality testing * * @author Nick…
Nick Gilbert
  • 4,159
  • 8
  • 43
  • 90
1
vote
3 answers

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

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.
kinokijuf
  • 968
  • 1
  • 11
  • 33
1
vote
1 answer

Time complexity of this primality testing algorithm?

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) …
0
votes
2 answers

Primality test for numbers of form 10^n + k

I have some numbers of form 10N + K, where N is about 1000, and K is really small (lower than 500). I want to test these numbers for primality. Currently I'm using Fermat's test by base 2, preceded by checking small factors (<10000). However, this…
Emily
  • 2,577
  • 18
  • 38
0
votes
0 answers

primality test function is_prime (n, k) based on Fermat Little Theorem,

I solved this question: Implement a primality test function is_prime (n, k) based on Fermat Little Theorem, where n is the number to be tested, and k is the number of bases to be used. The function should return False if n is not prime, and True if…
0
votes
2 answers

Java: read an int and check its primality. If it's not prime, repeat, but max x times

Sorry for such a question, but I just can't get through to it why the following code is not working as it is supposed to. As written in the title, a user should enter an integer for the code to check if it's prime. If it is not, the user gets to…
0
votes
2 answers

Why does my primality test stop after 1 and not seem to be able to go on?

I apologize if this is a dumb question, I'm pretty new to Java, but I can't figure out why my mod operator based primality test for numbers 1-100 stops at 1. I've tried following my code and I don't understand why it fails to continue to the last…
0
votes
3 answers

Converting isPrime() Python to Haskell

I have a working (albeit inefficient) function to check if a number is prime in Python and I'd like to convert it to Haskell. Efficiency doesn't matter to me right now as I am focusing primarily on making a readable algorithm in Haskell that uses…
Prithvi Boinpally
  • 427
  • 1
  • 9
  • 23
0
votes
1 answer

What is the best algorithm to determine if N is a prime number (if N is [2 <= N <= 2^63-1])?

I tried using the Miller-Rabin algorithm, but it can't detect very large numbers. #include #include #include long long mulmod(long long a, long long b, long long mod) { long long x = 0,y = a % mod; while (b >…
AB3
  • 67
  • 6
0
votes
0 answers

Why does python skip my while loop in my primality test when I enter a huge number?

I'm currently trying to make a simple primality test using python to test prime number. Here is the code I've made for the test: def is_prime(n): if np.mod(n, 2) == 0: return False f = 3 while np.square(f) <=…
The 2nd
  • 113
  • 6
0
votes
1 answer

Miller-Rabin Primality Test in python: Why I keep getting decimal.Overflow: []?

I was trying to do the Miller-Rabin Primality Test in python. I've written the code like below based on pseudocode on Wikipedia: from math import * from numpy import * def Miller_Rabin(n, k): #Miller-Rabin Primality Test if n == 2 or n ==…
The 2nd
  • 113
  • 6
0
votes
1 answer

Why do we check i <= sqrt(n) to find if a number is prime or not?

I know this question has been answered before, but I didn't quite understand the explanation that was given on that question. I was doing the 30 days of code on HackerRank, and one of the exercises was to check whether a number is prime or not.…
0
votes
0 answers

Testing for primality on a large integer that is stored as a string in C++

I have a program that calculates large numbers by storing them as strings, so that I can have very large digits that extend beyond long long. I can add the strings using a function I've written that models how we humans do addition by hand on paper,…
moreQthanAs
  • 1
  • 1
  • 4