primes

Finding prime numbers in Python

梦想的初衷 提交于 2019-12-11 16:46:42
问题 I need to write a function, is_prime() , which takes in an integer n > 1 and returns TRUE if the number is a prime number and False otherwise. But when I input 2, it always returns False . Is there anyway to correct this? def is_prime(x): if(x > 1): for i in range(2,x+1): if( x % i == 0): return False else: return True else: return False 回答1: Two issues: First issue is that range includes the number itself, which means that it will always return true (for numbers > 1) because prime numbers

Circular prime numbers

萝らか妹 提交于 2019-12-11 15:33:15
问题 I am trying to convert the following function which test the number if it's prime to another one that test if the integer is a circular prime. eg. 1193 is a circular prime, since 1931, 9311 and 3119 all are also prime. So i need to rotate the digits of the integer and test if the number is prime or not. any ideas? note: I am new to Haskell Programming isPrime :: Integer -> Bool isPrime 1 = False isPrime 2 = True isPrime n | (length [x | x <- [2 .. n-1], n `mod` x == 0]) > 0 = False |

Calculating primes using only odd divisors is slower

南楼画角 提交于 2019-12-11 15:28:35
问题 I've written a small C program to calculate primes and now try to optimize the code as far as I can. In the first revision of the program I was checking if a number was even (modulo 2) and if it was I would continue to the next number. In my second revision I tried only checking odd numbers to be possible primes by incrementing the number I would be checking by 2 (so I would start from 3, then check 5, then 7, then 9, then 11 etc etc). I thought this would be way faster as I had cut an extra

JavaScript: Out of Memory for huge result array in Sieve of Atkin implementation

微笑、不失礼 提交于 2019-12-11 15:13:37
问题 Currently I'm running some benchmarks between JavaScript, asm.js and WebAssembly. For that I've implemented a little program that searches for primes with the Sieve of Atkin algorithm. To make the ramp up negligible I'm calculating primes up to 500'000'000. My problem is, that the JavaScript implementation runs out of memory, because the result array becomes huge. That's my current implementation: const AMOUNT = 500000000; function getPrimes() { const limit = AMOUNT; const width = Math.sqrt

programming haskell code: List of primes

醉酒当歌 提交于 2019-12-11 13:59:06
问题 My task is to create a list of primes but only with the following information: -The list has to be infinite -I have to check up primes for n > 1 -If there is a variable 2 <= k < n-2 , which divides n ,then it is no prime, if it does not divide n , it is -No function! So I started writing a code like this: primes = [n| n<-[2,3..],k<-[2,3..], if (k>=2) && (k<=(n-2)) then if n `mod` k /= 0 then n else return () else return () ] But then appears the following error : "Couldn't match expected type

Why prolog outputs a weird tree-like list?

六眼飞鱼酱① 提交于 2019-12-11 13:49:15
问题 In this Prolog code I intend to list the first N primes, (...) biggerPrime(N,P) :- isPrime(N), P is N, !. biggerPrime(N,P) :- N1 = N+1, biggerPrime(N1,P). primeListAcc(0,A,R,R) :- !. primeList(N,L) :- primeListAcc(N,1,[],L). primeListAcc(N,A,L,R) :- N1 is N-1, biggerPrime(A,P), A1 is P+1, primeListAcc(N1,A1,[P|L],R). And it works fine if I want the list ordered backwards: ?- primeList(5,L). L = [11, 7, 5, 3, 2]. But if I change the last line of the code from [P|L] to [L|P] like this:

Python modifying wrong list?

99封情书 提交于 2019-12-11 11:51:10
问题 I'm trying to generate a list of primes using the this method. I need to loop through every number 2...n and check it for multiples of 2...n. For some reason, the wrong list seems to be getting modified. import sys import argparse import math parser = argparse.ArgumentParser(description='find the largest prime factor of a number') parser.add_argument('n', type=int, help='number') args = parser.parse_args() sieve = [] for i in range(2,args.n+1): sieve.append(i) # tried int(i) copy1 = sieve #

Powershell Multithreaded math

倾然丶 夕夏残阳落幕 提交于 2019-12-11 10:33:50
问题 I'm currently working on a self-inspired project to learn powershell, and have been writing a script to generate prime numbers. As it stands, the script works without issue, but my next goal is to increase it's processing speed. cls $Primes = @() $Primes += 3 $TargetNum = 5 $PrimesIndex = 0 $NumOfPrime = 3 while(1) { if(($TargetNum / 3) -lt 3) { $Primes += $TargetNum $TargetNum += 2 $NumOfPrime += 1 } else { if($Primes[$PrimesIndex] -le ($TargetNum / ($Primes[$PrimesIndex]))) { if($TargetNum

Is this an optimal prime generator?

隐身守侯 提交于 2019-12-11 10:16:46
问题 Is this in any way an optimal solution for finding primes? I am not trying to add every optimization under the sun, but is the principal good? def primesUpto(self, x): primes = [2] sieve = [2] i = 3 while i <= x: composite = False j = 0 while j < len(sieve): sieve[j] = sieve[j] - 1 if sieve[j] == 0: composite = True sieve[j] = primes[j] j += 1 if not composite: primes.append(i) sieve.append(i*i-i) i += 1 return primes 回答1: Hmm, very interesting. Your code is actual honest to goodness genuine

Prolog Find N prime numbers

偶尔善良 提交于 2019-12-11 07:49:29
问题 I have a problem with the recursive function of Prolog. I believe I am not implementing it right and need help. I need to generate the first N prime numbers and return it in a list. Generating the prime number is not an issue, but rather, generating it in a list is the issue I have. This is the part of the relevant code: genList(_, 0, _). genList(X, N, PrimeList, PrimeList):- N > 0, isprime(X), X1 is X +1, N1 is N -1, genList(X1,N1,[X|PrimeList], [X|PrimeList]),!. genList(X, N, PrimeList,