primes

How does this prime number test in Java work?

天大地大妈咪最大 提交于 2019-11-29 04:04:43
The code snippet below checks whether a given number is a prime number. Can someone explain to me why this works? This code was on a study guide given to us for a Java exam. public static void main(String[] args) { int j = 2; int result = 0; int number = 0; Scanner reader = new Scanner(System.in); System.out.println("Please enter a number: "); number = reader.nextInt(); while (j <= number / 2) { if (number % j == 0) { result = 1; } j++; } if (result == 1) { System.out.println("Number: " + number + " is Not Prime."); } else { System.out.println("Number: " + number + " is Prime. "); } } Overall

Finding prime factors

泄露秘密 提交于 2019-11-29 03:55:51
问题 #include <iostream> using namespace std; void whosprime(long long x) { bool imPrime = true; for(int i = 1; i <= x; i++) { for(int z = 2; z <= x; z++) { if((i != z) && (i%z == 0)) { imPrime = false; break; } } if(imPrime && x%i == 0) cout << i << endl; imPrime = true; } } int main() { long long r = 600851475143LL; whosprime(r); } I'm trying to find the prime factors of the number 600851475143 specified by Problem 3 on Project Euler (it asks for the highest prime factor, but I want to find all

Finding the nth prime number using Python

孤街浪徒 提交于 2019-11-29 02:42:01
When I run this code, even for just counting to the 10th prime number (instead of 1000) I get a skewed/jacked output--all "not prime" titles for my is_composite variable, my test_num is giving me prime and composite numbers, and my prime_count is off Some of the answers that developers have shared use functions and the math import--that's something we haven't yet covered. I am not trying to get the most efficient answer; I am just trying to write workable python code to understand the basics of looping. # test a prime by diving number by previous sequence of number(s) (% == 0). Do this by #

Why is this scala prime generation so slow/memory intensive?

房东的猫 提交于 2019-11-29 00:14:51
I run out of memory while finding the 10,001th prime number. object Euler0007 { def from(n: Int): Stream[Int] = n #:: from(n + 1) def sieve(s: Stream[Int]): Stream[Int] = s.head #:: sieve(s.filter(_ % s.head != 0)) def primes = sieve(from(2)) def main(args: Array[String]): Unit = { println(primes(10001)) } } Is this because after each "iteration" (is this the correct term in this context?) of primes , I increase the stack of functions to be called to get the next element by one? One solution that I've found on the web which doesn't resort to an iterative solution (which I'd like to avoid to

Sample code for fast primality testing in C# [duplicate]

强颜欢笑 提交于 2019-11-28 23:31:21
问题 Possible Duplicate: Fastest algorithm for primality test Would appreciate a reference to sample code for fast primality testing in C#, preferably using BigInteger or other variable size type. 回答1: This is a Miller Rabin test in c#: bool MillerRabin(ulong n) { ulong[] ar; if (n < 4759123141) ar = new ulong[] { 2, 7, 61 }; else if (n < 341550071728321) ar = new ulong[] { 2, 3, 5, 7, 11, 13, 17 }; else ar = new ulong[] { 2, 3, 5, 7, 11, 13, 17, 19, 23 }; ulong d = n - 1; int s = 0; while ((d & 1

Arcane isPrime method in Java

久未见 提交于 2019-11-28 21:38:34
Consider the following method: public static boolean isPrime(int n) { return ! (new String(new char[n])).matches(".?|(..+?)\\1+"); } I've never been a regular expression guru, so can anyone fully explain how this method actually works? Furthermore , is it efficient compared to other possible methods for determining whether an integer is prime? First, note that this regex applies to numbers represented in a unary counting system, i.e. 1 is 1 11 is 2 111 is 3 1111 is 4 11111 is 5 111111 is 6 1111111 is 7 and so on. Really, any character can be used (hence the . s in the expression), but I'll use

Very simple prime number test - I think I'm not understanding the for loop

淺唱寂寞╮ 提交于 2019-11-28 21:31:15
I am practicing past exam papers for a basic java exam, and I am finding it difficult to make a for loop work for testing whether a number is prime. I don't want to complicate it by adding efficiency measures for larger numbers, just something that would at least work for 2 digit numbers. At the moment it always returns false even if n IS a prime number. I think my problem is that I am getting something wrong with the for loop itself and where to put the "return true;" and "return false;"... I'm sure it's a really basic mistake I'm making... public boolean isPrime(int n) { int i; for (i = 2; i

Problems with prime numbers

左心房为你撑大大i 提交于 2019-11-28 20:56:59
I am trying to write a program to find the largest prime factor of a very large number, and have tried several methods with varying success. All of the ones I have found so far have been unbelievably slow. I had a thought, and am wondering if this is a valid approach: long number = input; while(notPrime(number)) { number = number / getLowestDivisiblePrimeNumber(); } return number; This approach would take an input, and would do the following: 200 -> 100 -> 50 -> 25 -> 5 (return) 90 -> 45 -> 15 -> 5 (return) It divides currentNum repeatedly by the smallest divisible number (most often 2, or 3)

Is there a way to find the approximate value of the nth prime?

妖精的绣舞 提交于 2019-11-28 20:40:31
Is there a function which will return the approximate value of the n th prime? I think this would be something like an approximate inverse prime counting function. For example, if I gave this function 25 it would return a number around 100, or if I gave this function 1000 it would return a number around 8000. I don't care if the number returned is prime or not, but I do want it to be fast (so no generating the first n prime numbers to return the n th.) I would like this so that I can generate the first n prime numbers using a sieve ( Eratosthenes or Atkin ). Therefore, the approximation for n

How many iterations of Rabin-Miller should I use for cryptographic safe primes?

馋奶兔 提交于 2019-11-28 20:32:20
问题 I am generating a 2048-bit safe prime for a Diffie-Hellman-type key, p such that p and (p-1)/2 are both prime. How few iterations of Rabin-Miller can I use on both p and (p-1)/2 and still be confident of a cryptographically strong key? In the research I've done I've heard everything from 6 to 64 iterations for 1024-bit ordinary primes, so I'm a little confused at this point. And once that's established, does the number change if you are generating a safe prime rather than an ordinary one?