primes

Generate prime number using OpenSSL

橙三吉。 提交于 2019-12-19 02:08:11
问题 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

SQL Prime number function

不打扰是莪最后的温柔 提交于 2019-12-18 21:48:30
问题 If I have a number X and want to say IsPrime(X) = true/false using sql-server what is the best approach? Do I just import a table of primes or is there an algorithm that is fairly efficient for the smaller primes? Note: I'm not interested in numbers greater than approx. 10 million. Ended up using the following: CREATE FUNCTION [dbo].[isPrime] ( @number INT ) RETURNS VARCHAR(10) BEGIN DECLARE @retVal VARCHAR(10) = 'TRUE'; DECLARE @x INT = 1; DECLARE @y INT = 0; WHILE (@x <= @number ) BEGIN IF

How to find out if two numbers are relatively prime?

霸气de小男生 提交于 2019-12-18 19:42:05
问题 I'm trying to write a method that will calculate if two numbers are relatively prime for an assignment. I'm primarily looking for answers on where to start. I know there is a method gcd() that will do a lot of it for me, but the assignment is pretty much making me do it without gcd or arrays. I kind of have it started, because I know that I will have to use the % operator in a for loop. public static boolean relativeNumber(int input4, int input5){ for(int i = 1; i <= input4; i++) Obviously

Return all prime numbers smaller than M

不打扰是莪最后的温柔 提交于 2019-12-18 16:46:53
问题 Given an integer M. return all prime numbers smaller than M. Give a algorithm as good as you can. Need to consider time and space complexity. 回答1: A couple of additional performance hints: You only need to test up to the square root of M , since every composite number has at least one prime factor less than or equal to its square root You can cache known primes as you generate them and test subsequent numbers against only the numbers in this list (instead of every number below sqrt(M) ) You

Haskell prime test

China☆狼群 提交于 2019-12-18 15:53:24
问题 I'm new to Haskell, and I'm trying a bit: isPrime :: Integer->Bool isPrime x = ([] == [y | y<-[2..floor (sqrt x)], mod x y == 0]) I have a few questions. Why when I try to load the .hs, WinHugs say: Instances of (Floating Integer, RealFrac Integer) required for definition of isPrime ? When the interpreter finds one element in the right set, it immediately stops or it computes all the set? I think you know what I mean. Sorry about my english. 回答1: 1) The problem is that sqrt has the type

Miller Rabin Primality test accuracy

一世执手 提交于 2019-12-18 13:36:18
问题 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)? 回答1: 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

Python Eratosthenes Sieve Algorithm Optimization

久未见 提交于 2019-12-18 09:17:45
问题 I'm attempting to implement the Sieve of Eratosthenes. The output seems to be correct (minus "2" that needs to be added) but if the input to the function is larger than 100k or so it seems to take an inordinate amount of time. What are ways that I can optimize this function? def sieveErato(n): numberList = range(3,n,2) for item in range(int(math.sqrt(len(numberList)))): divisor = numberList[item] for thing in numberList: if(thing % divisor == 0) and thing != divisor: numberList.remove(thing)

Using a larger prime as a multiplier when overriding hashCode()

狂风中的少年 提交于 2019-12-18 03:22:30
问题 I have been reading about hashcode functions for the past couple of hours and have accumulated a couple of questions regarding use of prime numbers as multipliers in custom hashcode implementations. I would be grateful if I could get some insight regarding following questions: In a comment to @mattb's answer here, @hstoerr advocates for use of larger primes (such as 524287) instead of the common prime 31. My question is, given the following implementation of a hashcode functions for a pair or

Using a larger prime as a multiplier when overriding hashCode()

China☆狼群 提交于 2019-12-18 03:22:08
问题 I have been reading about hashcode functions for the past couple of hours and have accumulated a couple of questions regarding use of prime numbers as multipliers in custom hashcode implementations. I would be grateful if I could get some insight regarding following questions: In a comment to @mattb's answer here, @hstoerr advocates for use of larger primes (such as 524287) instead of the common prime 31. My question is, given the following implementation of a hashcode functions for a pair or

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

两盒软妹~` 提交于 2019-12-17 23:32:43
问题 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