primes

finding the running time for my algorithm for finding whether an input is prime in terms of the input

為{幸葍}努か 提交于 2019-12-02 18:43:04
问题 This is my function for finding prime numbers void print(int num) { for(int i=2; i<num/2; i++) { if(num%i==0) { cout<<"not prime\n"; exit(0); } } cout<<"prime\n"; } My input in num. I'm trying to find the runtime using big oh. I remember that finding the run time had something to do with log. The worst case would be that my program would run the n/2 -1 times? 回答1: Yes, the loop runs n/2-1 times and only contains commands of constant complexity, so your runtime scales as a*(n/2-1) for some a.

Fastest modular exponentiation in JavaScript

左心房为你撑大大i 提交于 2019-12-02 18:30:15
My problem is to compute (g^x) mod p quickly in JavaScript, where ^ is exponentiation, mod is the modulo operation. All inputs are nonnegative integers, x has about 256 bits, and p is a prime number of 2048 bits, and g may have up to 2048 bits. Most of the software I've found that can do this in JavaScript seems to use the JavaScript BigInt library ( http://www.leemon.com/crypto/BigInt.html ). Doing a single exponentiation of such size with this library takes about 9 seconds on my slow browser (Firefox 3.0 with SpiderMonkey). I'm looking for a solution which is at least 10 times faster. The

Is Swift really slow at dealing with numbers?

和自甴很熟 提交于 2019-12-02 17:04:08
As I was playing around with a swift tutorial, I started to write a custom isPrime method to check if a given Int is prime or not. After writing it I realized it was working properly but found it a bit slow to perform isPrime on some quite large numbers (still much lower then Int.max ). So I wrote the same piece of code in objc and the code was executed much faster (a factor of 66x). Here is the swift code: class Swift { class func isPrime(n:Int) -> Bool { let sqr : Int = Int(sqrt(Double(n))) + 1 for i in 2...sqr { if n % i == 0 { return false } } return true; } class func primesInRange(start

C++ code for checking for prime numbers not working

▼魔方 西西 提交于 2019-12-02 16:44:45
问题 I'm having trouble with this C++ code. The integer num is a number that I want to check if it is prime. However this program is always returning false. It's probably something simple but I can't find anything. for(int i=2;i<num;i++){ //primes are allowed to be divided by 1 so we start at 2 if(num % i == 0){ //can be divided by a number other than itself or 1 so we trip out return false; } else if(i == num){ //if we've already done checks as high as possible and not tripped out yet then report

Lazy List of Prime Numbers

荒凉一梦 提交于 2019-12-02 16:41:15
How would one implement a list of prime numbers in Haskell so that they could be retrieved lazily? I am new to Haskell, and would like to learn about practical uses of the lazy evaluation functionality. Niki Here's a short Haskell function that enumerates primes from Literate Programs: primes :: [Integer] primes = sieve (2 : [3, 5..]) where sieve (p:xs) = p : sieve [x|x <- xs, x `mod` p > 0] Apparently, this is not the Sieve of Eratosthenes (thanks, Landei). I think it's still an instructive example that shows you can write very elegant, short code in Haskell and that shows how the choice of

Finding a prime number after a given number

梦想与她 提交于 2019-12-02 16:36:17
How can I find the least prime number greater than a given number? For example, given 4, I need 5; given 7, I need 11. I would like to know some ideas on best algorithms to do this. One method that I thought of was generate primes numbers through the Sieve of Eratosthenes, and then find the prime after the given number. Some other methods have been suggested and I think that they are good, but it really depends on how much you want to have to store or compute on the spot. For instance if you are looking for the next prime after a very large number, then using the Sieve of Eratosthenes might

Why is the size 127 (prime) better than 128 for a hash-table?

大憨熊 提交于 2019-12-02 14:50:51
Supposing simple uniform hashing, that being, any given value is equally like to hash into any of the slots of the hash. Why is it better to use a table of size 127 and not 128? I really don't understand what's the problem with the power of 2 numbers. Or how it actually makes any difference at all. When using the division method, we usually avoid certain values of m (table size). For example, m should not be a power of 2, since if m = 2^p , then h(k) is just the p lowest-order bits of k. Let's suppose the possible elements are only between 1 and 10000 and I picked the table size as 128. How

My For loop won't iterate through a list

橙三吉。 提交于 2019-12-02 14:40:49
I have to determine if all the numbers in a list are prime numbers and then return a boolean "True" or "False" statement depending on the outcome. I made some conditional statements inside of a for loop to see if the number was prime or not. Here's the code: def all_primes(xs): is_prime = None for i in xs: if i < 2: is_prime = False return is_prime break elif (i % 2 == 0) and (i % i == 1): is_prime = False return is_prime break else: is_prime = True return is_prime The problem is, and I saw this in the Python Visualizer, the for loop stops iterating after checking the first value in the list.

Sieve of Eratosthenes without arrays?

僤鯓⒐⒋嵵緔 提交于 2019-12-02 13:23:28
问题 I have to write a java code for the 'sieve of eratosthenes' algorithm to print out primes up to a given max value on the console but I'm not allowed to use arrays. Our professor told us it is possible to do only with the help of loops. So I thought a lot and googled a lot about this topic and couldn't find an answer. I dont think it's possible at all because you have store the information which digits are already crossed out somewhere. my code until now: public static void main(String[] args)

C++ code for checking for prime numbers not working

旧城冷巷雨未停 提交于 2019-12-02 12:12:45
I'm having trouble with this C++ code. The integer num is a number that I want to check if it is prime. However this program is always returning false. It's probably something simple but I can't find anything. for(int i=2;i<num;i++){ //primes are allowed to be divided by 1 so we start at 2 if(num % i == 0){ //can be divided by a number other than itself or 1 so we trip out return false; } else if(i == num){ //if we've already done checks as high as possible and not tripped out yet then report success return true; } } i == num will never occur, since your loop condition is i<num . Try: for(int