primes

Fast algorithm for finding prime numbers? [duplicate]

此生再无相见时 提交于 2019-11-30 20:10:39
This question already has an answer here: Which is the fastest algorithm to find prime numbers? 14 answers First of all - I checked a lot in this forum and I haven't found something fast enough . I try to make a function that returns me the prime numbers in a specified range. For example I did this function (in C#) using the sieve of Eratosthenes. I tried also Atkin's sieve but the Eratosthenes one runs faster (in my implementation): public static void SetPrimesSieve(int Range) { Primes = new List<uint>(); Primes.Add(2); int Half = (Range - 1) >> 1; BitArray Nums = new BitArray(Half, false);

Generate prime number using OpenSSL

拥有回忆 提交于 2019-11-30 19:55:29
How can I generate a large random prime using openssl, I found out how to generate a random number and check if it is prime but I have not been able to automate the process of checking the primality, here is the command that i am using: openssl rand -hex 256 | xargs openssl prime -hex Should I use a while loop to repeatedly check if the result is prime? How can I automate the process of checking if the result does not contain the keyword "not", This is all the further i got on writing the while loop: while [{openssl rand -hex 256 | xargs openssl prime -hex} = *"$not"*] OpenSSL version 1.0.0

Prime Number Generator Logic

天大地大妈咪最大 提交于 2019-11-30 19:14:43
问题 I am supposed to make a class PrimeNumberGenerator which has a method nextPrime that will print out all prime numbers up to a number the user inputs. Ex) Enter a Number: 20 2 3 5 7 11 13 17 19 Our teacher told us that we should use a nested for loop. I tried, but when I tried to make the inner (nested) loop, I got really confused. Here is my code: (I'm going to make a tester class later) public class PrimeGenerator { private int num; boolean isPrime; public PrimeGenerator(int n) { num = n; }

Why is this prime test so slow?

笑着哭i 提交于 2019-11-30 18:40:45
This code was taken from the book "Haskell Road to Logic, Math and Programming". It implements sieve of eratosthenes algorithm and solves Project Euler Problem 10. sieve :: [Integer] -> [Integer] sieve (0 : xs) = sieve xs sieve (n : xs) = n : sieve (mark xs 1 n) where mark :: [Integer] -> Integer -> Integer -> [Integer] mark (y:ys) k m | k == m = 0 : (mark ys 1 m) | otherwise = y : (mark ys (k+1) m) primes :: [Integer] primes = sieve [2..] -- Project Euler #10 main = print $ sum $ takeWhile (< 2000000) primes Actually it runs even slower then the naive prime test. Can someone explain this

Python- Sieve of Eratosthenes- Compact Python

南笙酒味 提交于 2019-11-30 18:24:20
问题 This is my code for finding primes using the Sieve of Eratosthenes. list = [i for i in range(2, int(raw_input("Compute primes up to what number? "))+1)] for i in list: for a in list: if a!=i and a%i == 0: list.remove(a) Trying to find a way to compress those nested for loops into some kind of generator or comprehension, but it doesn't seem that you can apply a function to a list using a comprehension. I tried using map and filter, but I can't seem to get it right. Thinking about something

Stack space overflow when computing primes

99封情书 提交于 2019-11-30 17:21:46
问题 I'm working my way through Real World Haskell (I'm in chapter 4) and to practice a bit off-book I've created the following program to calculate the nth prime. import System.Environment isPrime primes test = loop primes test where loop (p:primes) test | test `mod` p == 0 = False | p * p > test = True | otherwise = loop primes test primes = [2, 3] ++ loop [2, 3] 5 where loop primes test | isPrime primes test = test:(loop primes' test') | otherwise = test' `seq` (loop primes test') where test' =

Prime Factorization Program in Java

╄→гoц情女王★ 提交于 2019-11-30 16:48:05
I am working on a prime factorization program implemented in Java. The goal is to find the largest prime factor of 600851475143 ( Project Euler problem 3 ). I think I have most of it done, but I am getting a few errors. Also my logic seems to be off, in particular the method that I have set up for checking to see if a number is prime. public class PrimeFactor { public static void main(String[] args) { int count = 0; for (int i = 0; i < Math.sqrt(600851475143L); i++) { if (Prime(i) && i % Math.sqrt(600851475143L) == 0) { count = i; System.out.println(count); } } } public static boolean Prime

Comparing anagrams using prime numbers

北战南征 提交于 2019-11-30 15:40:04
问题 There is the problem of trying to see if two unique strings are anagrams of each other. The first solution that I had considered would be to sort both strings and see if they were equal to each other. I have been considering another solution and I would like to discuss if the same would be feasible. The idea would be to assign a numerical value to each character and sum it up such that a unique set of characters would produce a unique value. As we are testing for anagrams, we do not mind if

Efficiently check if two numbers are co-primes (relatively primes)?

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-30 15:10:00
问题 What is the most efficient ("pythonic") way to test/check if two numbers are co-primes (relatively prime) in Python. For the moment I have this code: def gcd(a, b): while b != 0: a, b = b, a % b return a def coprime(a, b): return gcd(a, b) == 1 print(coprime(14,15)) #Should be true print(coprime(14,28)) #Should be false Can the code for checking/testing if two numbers are relatively prime be considered "Pythonic" or there is some better way? 回答1: The only suggestion for improvement might be

Comparing anagrams using prime numbers

本秂侑毒 提交于 2019-11-30 14:14:31
There is the problem of trying to see if two unique strings are anagrams of each other. The first solution that I had considered would be to sort both strings and see if they were equal to each other. I have been considering another solution and I would like to discuss if the same would be feasible. The idea would be to assign a numerical value to each character and sum it up such that a unique set of characters would produce a unique value. As we are testing for anagrams, we do not mind if the checksum of "asdf" and "adsf" are the same -- in fact, we require it to be that way. However the