primes

Why is this Haskell code snippet not infinitely recursive?

妖精的绣舞 提交于 2019-12-05 00:27:29
To help me learn Haskell, I am working through the problems on Project Euler. After solving each problem, I check my solution against the Haskell wiki in an attempt to learn better coding practices. Here is the solution to problem 3 : primes = 2 : filter ((==1) . length . primeFactors) [3,5..] primeFactors n = factor n primes where factor n (p:ps) | p*p > n = [n] | n `mod` p == 0 = p : factor (n `div` p) (p:ps) | otherwise = factor n ps problem_3 = last (primeFactors 317584931803) My naive reading of this is that primes is defined in terms of primeFactors , which is defined in terms of primes

Count number of non-prime pairs that when multiplied form a given number N,

妖精的绣舞 提交于 2019-12-04 22:48:50
A non-prime pair which forms N is 2 different non-prime numbers where the product of the numbers is N. 1<=N<=10^6 For example For N = 24 there are 2 good pairs (non-prime pairs that form N) (4,6), (1,24), but (2,12), (3,8) are not good. Note: for any 2 numbers a and b pair(a,b) = pair(b,a). There is another condition which states that if the number is a special number, so output = -1 otherwise count the number of non-primes. Number is called special number if it can be represented as a product of three prime numbers . Example: 12 is a special number because 12=2*2*3; I tried brute-force

Is there an upper limit on .txt file size?

走远了吗. 提交于 2019-12-04 22:38:27
As a Christmas gift I have written a small program in Java to calculate primes. My intention was to leave it on all night, calculating the next prime and writing it to a .txt file. In the morning I would kill the program and take the .txt file to my friend for Christmas. Is there anything I should be worried about? Bear in mind that this is true beginner Ziggy you are talking to, not some smart error checking ASM guy. EDIT More specifically, since I will be leaving this program on all night counting primes, is there any chance at all that I will encounter some kind of memory related error?

Prime Numbers Optimization in C [closed]

好久不见. 提交于 2019-12-04 21:19:55
I try to print the prime numbers; 2 to 1 million. But nothing printed on the console. Could you check my code? And how can I be this code more optimized? Here's my code: #include <stdio.h> #include <math.h> main() { int num, sr, num2; for (num = 2; num <= 1000000; num++) { sr = (int) sqrt(num); for (num2 = 2; sr % num2 != 0; num2++) { if (sr == num2) { printf("%d\n", sr); } } } } #include <stdio.h> #include <math.h> int main(){ int num, sr, num2; int isPrime = 1; // this is a check parameter; isPrime = 0 if number is not prime. for(num=2; num<=100; num++){ sr = (int) sqrt(num); for(num2=2;

Optimization of Algorithm [closed]

╄→гoц情女王★ 提交于 2019-12-04 16:48:18
Here is the link to the problem. The problem asks the number of solutions to the Diophantine equation of the form 1/x + 1/y = 1/z (where z = n! ). Rearranging the given equation clearly tells that the answer is the number of factors of z 2 . So the problem boils down to finding the number of factors of n! 2 ( n factorial squared). My algorithm is as follows Make a Boolean look up table for all primes <= n using Sieve of Eratosthenes algorithm. Iterate over all primes P <= n and find its exponent in n! . I did this using step function formula. Let the exponent be K , then the exponent of P in n

Finding first n primes? [duplicate]

偶尔善良 提交于 2019-12-04 16:17:39
This question already has answers here : Closed 8 years ago . Possible Duplicate: Fastest way to list all primes below N in python Although I already have written a function to find all primes under n ( primes(10) -> [2, 3, 5, 7] ), I am struggling to find a quick way to find the first n primes. What is the fastest way to do this? Olhovsky Start with the estimate g(n) = n log n + n log log n *, which estimates the size of the nth prime for n > 5. Then run a sieve on that estimate. g(n) gives an overestimate, which is okay because we can simply discard the extra primes generated which are

What does “e is 65537 (0x10001)” mean?

℡╲_俬逩灬. 提交于 2019-12-04 15:35:34
问题 I want to know what the output e is 65537 (0x10001) means. It happens during the RSA Key Generation using openssl genrsa . I know that the dots mean that the number has passed a probe division and the plus is printed out after it passed a miller rabin test. But i can't figure out what the last info message before the RSA key is printed out means. I can't find it in the openssl docs. And I could use it in term paper on prime number generation. Thanks for your help! :) 回答1: The "e" is the

How TDD works when there can be millions of test cases for a production functionality?

核能气质少年 提交于 2019-12-04 15:19:26
问题 In TDD, you pick a test case and implement that test case then you write enough production code so that the test passes, refactor the codes and again you pick a new test case and the cycle continues. The problem I have with this process is that TDD says that you write enough code only to pass the test you just wrote. What I refer to exactly is that if a method can have e.g. 1 million test cases, what can you do?! Obviously not writing 1 million test cases?! Let me explain what I mean more

Prime Generator Algorithm

只愿长相守 提交于 2019-12-04 14:32:02
I've been trying to solve the SPOJ problem of Prime number Generator Algorithm. Here is the question 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. Output For every test case print all prime numbers p such that m <= p <= n, one number per line, test cases separated by an empty line. It is very easy,

Super Ugly Number

喜你入骨 提交于 2019-12-04 14:12:15
So the problem is: Write a program to find the nth super ugly number. Super ugly numbers are positive numbers whose all prime factors are in the given prime list primes of size k. For example, [1, 2, 4, 7, 8, 13, 14, 16, 19, 26, 28, 32] is the sequence of the first 12 super ugly numbers given primes = [2, 7, 13, 19] of size 4. So my algorithm basically finds all possible factors using the pattern they follow, pushes them to an array, sorts that array and then returns the nth value in the array. It accurately calculates all of them, however, is too slow with high nth values. My question is what