primes

RSA and prime-generator algorithms

女生的网名这么多〃 提交于 2019-12-20 09:44:06
问题 OK, my understanding of the mathematical workings of RSA may not be as deep as it should, so feel free to slap me over the head if this is stupid: To generate a private key, we need two random big primes. There is no algorithm that can do that precisely and efficiently, but there are algorithms that can generate big numbers that have a 99.99999...(a bazillion 9s)...999% probability of being prime. My question is: what happens if, by a phenomenal stroke of bad luck, when you were generating

Lazy List of Prime Numbers

徘徊边缘 提交于 2019-12-20 08:40:29
问题 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. 回答1: 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

My For loop won't iterate through a list

眉间皱痕 提交于 2019-12-20 07:49:29
问题 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

Find the highest prime number in a given range

人盡茶涼 提交于 2019-12-20 06:43:10
问题 I need to find the highest prime number in a given range. Here is my code which works for 0-100 but if I give 0-125 it is showing prime number as 125. <?php $flag=0; $b=125; for($i=$b;$i>=0;$i--) { if($i%2!=0) { for($b=3;$b<10;$b++) { if($flag==0) { echo('<br>'); if($i%$b!=0) { echo('highest prime number is'.$i); $flag=1; break; } elseif ($i%$b==0) { break; } } } } } ?> In the above code I have taken the range from 0-125 回答1: Yup the problem is algorithmic... 1) You'll need to check up till

printing out prime numbers from array

老子叫甜甜 提交于 2019-12-20 06:28:48
问题 I'd like to print out all prime numbers from an array with method. I can do it with one int but don't know how to return certain numbers from array. Thanks for help! public static boolean isPrime(int [] tab) { boolean prime = true; for (int i = 3; i <= Math.sqrt(tab[i]); i += 2) if (tab[i] % i == 0) { prime = false; break; } for(int i=0; i<tab.length; i++) if (( tab[i]%2 !=0 && prime && tab[i] > 2) || tab[i] == 2) { return true; } else { return false; } //return prime; } thanks both of you.

How can I improve this code for Project Euler 7?

六月ゝ 毕业季﹏ 提交于 2019-12-20 04:03:56
问题 By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13. What is the 10 001st prime number? My solution: public class Prime_Number { public static boolean isPrime(long n) { if ((n > 2 && n % 2 == 0) || (n > 3 && n % 3 == 0) || (n > 5 && n % 5 == 0) || n == 0 || n == 1) { return false; } return true; } public static void main(String[] args) { int count = 0; int prime = 0; while (prime <= 10001) { if (isPrime(count) == true) { prime++; if (prime ==

How come in this code to find prime numbers, is_prime(9) returns True? [duplicate]

旧街凉风 提交于 2019-12-20 03:59:06
问题 This question already has answers here : Program that checks if a number is prime number (5 answers) Closed 11 months ago . def is_prime(x): if x < 2: return False else: for n in range(2, x): if x % n == 0: return False else: return True print is_prime(9) returns True instead of False . I don't quite understand. The range (2,9) includes this list: 2,3,4,5,6,7,8 and 9 % 3 == 0 , So how come I do not get False as the answer of that function? 回答1: This is because you don't actually loop, as you

Function to return prime numbers

ⅰ亾dé卋堺 提交于 2019-12-20 03:52:48
问题 I want to write a function in R which accepts a list of integers and returns only the values which are prime. So far I have this: primefindlist<-function(n){ return(n[n==2 | all(n %% seq(2,ceiling(sqrt(n)),by=1) !=0)]) } But I keep getting an error message when I run the function e.g; primefindlist(c(7,11)) Error in seq.default(2, ceiling(sqrt(n)), by = 1) : 'to' must be of length 1 Anyone got any ideas how to overcome this? Also the code below tells me if a single integer is prime or not ie

R: Split data frame when number of columns is a prime

耗尽温柔 提交于 2019-12-20 03:23:43
问题 I have a data.frame that has 131 columns. I need to part this into groups of about 10 to 15 variables (i.e., splitting by column, not by row!). Obviously, as 131 is a prime number, not all the new dataframes can be of equal length... I searched for an answer in the posts How to cut data in even pieces in R? Split a vector into chunks in R Splitting a large vector into intervals in R But they all seem to assume that the new data frames are of equal size. EDIT thanks to the comments below, I

Check for a prime number using recursive helper function

好久不见. 提交于 2019-12-20 02:56:14
问题 I am trying to check if a number is prime using recursion. I was required to use a recursive helper function, but I am not sure how I should implement it. I think I know the algorithm, but I've never tried to use a recursive helper function in Racket. This is my current thoughts: See if n is divisible by i = 2 Set i = i + 1 If i^2 <= n continue. If no values of i evenly divided n , then it must be prime. This is what I have so far... (define (is_prime n) (if (<= n 1) #f (if (= (modulo n 2) 0)