primes

Implementation of Fermat's primality test

≡放荡痞女 提交于 2019-12-19 10:49:26
问题 Who wants to help me with my homework? I'm try to implement Fermat's primality test in Java using BigIntegers. My implementation is as follows, but unfortunately it doesn't work. Any ideas? public static boolean checkPrime(BigInteger n, int maxIterations) { if (n.equals(BigInteger.ONE)) return false; BigInteger a; Random rand = new Random(); for (int i = 0; i < maxIterations; i++) { a = new BigInteger(n.bitLength() - 1, rand); a = a.modPow(n.subtract(BigInteger.ONE), n); if (!a.equals

Sieve of Eratosthenes using precalculated primes

左心房为你撑大大i 提交于 2019-12-19 10:25:42
问题 I've all prime numbers that can be stored in 32bit unsigned int and I want to use them to generate some 64bit prime numbers . using trial division is too slow even with optimizations in logic and compilation. I'm trying to modify Sieve of Eratosthenes to work with the predefined list, as follow: in array A from 2 to 4294967291 in array B from 2^32 to X inc by 1 find C which is first multiple of current prime. from C mark and jump by current prime till X. go to 1. The problem is step 3 which

Factorizing a number in Python

↘锁芯ラ 提交于 2019-12-19 09:16:10
问题 Here's my code: def factorize(n): sieve = [True] * (n + 1) for x in range(2, int(len(sieve) ** 0.5) + 1): if sieve[x]: for i in range(x + x, len(sieve), x): sieve[i] = False lowerPrimes = i for i in range(2, len(sieve)) if sieve[i]] and (n % i == 0)] return lowerPrimes factorize(n) returns all prime factors of the given value n . As you can see, it first makes an Eratosthenes sieve for n and then uses a list comprehension to return all values in the sieve that are factors of n . It works

Factorizing a number in Python

夙愿已清 提交于 2019-12-19 09:16:05
问题 Here's my code: def factorize(n): sieve = [True] * (n + 1) for x in range(2, int(len(sieve) ** 0.5) + 1): if sieve[x]: for i in range(x + x, len(sieve), x): sieve[i] = False lowerPrimes = i for i in range(2, len(sieve)) if sieve[i]] and (n % i == 0)] return lowerPrimes factorize(n) returns all prime factors of the given value n . As you can see, it first makes an Eratosthenes sieve for n and then uses a list comprehension to return all values in the sieve that are factors of n . It works

Haskell: faster summation of primes

情到浓时终转凉″ 提交于 2019-12-19 08:29:09
问题 Disclaimer: I'm working on Euler Problem 9. I'm adding up some pretty large numbers, all the primes from 1 to 2 000 000. Summing those primes takes forever. I'm using the haskell built in function 'sum'. as in: sum listOfPrimes Are there any other faster options? --My prime generator was the slow link in my code. 回答1: It sounds like your problem is not summing the numbers, but generating them. What is your implementation of listOfPrimes? This paper may be of interest: http://lambda-the

Program to find all primes in a very large given range of integers

我的未来我决定 提交于 2019-12-19 07:54:11
问题 i came across this following question on a programming website : Peter wants to generate some prime numbers for his cryptosystem. Help him! Your task is to generate all prime numbers between two given numbers! Input The input begins with the number t of test cases in a single line (t<=10). In each of the next t lines there are two numbers m and n (1 <= m <= n <= 1000000000, n-m<=100000) separated by a space. I came up with the following solution : import java.util.*; public class PRIME1 {

Why is multiplied many times faster than taking the square root?

天大地大妈咪最大 提交于 2019-12-19 04:56:20
问题 I have several questions with the following algorithms to tell if a number is prime, I also know that with the sieve of Eratosthenes can be faster response. Why is faster to compute i i * sqrt (n) times. than sqrt (n) just one time ? Why Math.sqrt() is faster than my sqrt() method ? What is the complexity of these algorithms O (n), O (sqrt (n)), O (n log (n))? public class Main { public static void main(String[] args) { // Case 1 comparing Algorithms long startTime = System.currentTimeMillis(

Faster way to check if a number is a prime? [duplicate]

随声附和 提交于 2019-12-19 03:19:05
问题 This question already has answers here : Program to find prime numbers (25 answers) Fastest algorithm for primality test [closed] (10 answers) Closed 6 years ago . I got this code that checks if a number is a prime: public static bool isPrime(int num) { if (num == 1) return false; if (num == 2) return true; int newnum = Math.Floor(Math.Sqrt(num)); for (int i = 2; i <= newnum; i++) if (num % i == 0) return false; return true; } Is there any better and faster way to check if a number is a prime

Prime factorization of a factorial

拟墨画扇 提交于 2019-12-19 03:12:40
问题 I need to write a program to input a number and output its factorial's prime factorization in the form: 4!=(2^3)*(3^1) 5!=(2^3)*(3^1)*(5^1) The problem is I still can't figure out how to get that result. Apparently each first number in brackets is for the ascending prime numbers up until the actual factorial. The second number in brackets is the amount of times the number occurs in the factorial. What I can't figure out is for example in 5!=(2^3)*(3^1)*(5^1) , how does 2 only occur 3 times, 3

Primality test in python [duplicate]

帅比萌擦擦* 提交于 2019-12-19 02:31:33
问题 This question already has answers here : How to create the most compact mapping n → isprime(n) up to a limit N? (28 answers) Closed 4 years ago . I'm trying to do a simple primality test in Python. Accoding to Wikipedia, a primality test is the following: Given an input number n, check whether any integer m from 2 to n − 1 divides n. If n is divisible by any m then n is composite, otherwise it is prime. I started with ruling out the even numbers - with the exception of 2 - as candidates to