Store the result of a function as a variable - python

I'm trying to write an RSA code, but I'm having issues with a simple thing. I want to store the result of a function as a variable, twice in Python. This is my code
def random_odd_between(j,k):
k1 = (k-1)//2
j1 = j//2
a1 = int(random()*(k1-j1+1))+j1
a = 2*a1 +1
return a
# The list of primes less than 10^6 is given by:
list_of_primes = prime_range(1,10^6)
# Write a function to check if given number is prime:
def check_if_prime(n):
prime_flag = 1
for i in list_of_primes:
checker = n%i
if checker != 0:
if (n/i) in ZZ:
prime_flag = 0
break
else:
prime_flag = 0
break
return prime_flag
# Generate random numbers between 6*10^9 and 10^10 until we get a prime.
# Generate two prime numbers between 6*10^9 and 10^10 for p and q
def get_a_prime():
count = 0
prime_found = 0
while prime_found == 0:
a = random_odd_between(6*10^9,10^10)
prime_found = check_if_prime(a)
count = count + 1
# Print a prime you've got:
print '%i' %a
p = get_a_prime()
q = get_a_prime()
n = p*q
# Let t stand for totient
t = (p-1)*(q-1)
I can't get my p and q to be defined however, they keep just giving me an error. I realize I need to do some kind of return, but I can't get the syntax right

just replace print '%i' %a with return a

I believe you had errors in both your check_if_prime and get_a_prime functions. In the former, ZZ is not defined and the first break should be indented one more level and the last one is redundant. Better yet, just return True or False when needed.
In the second function, you need to return the value that is prime rather than just print it.
def check_if_prime(n):
if n == 2:
return True # 2 is a prime.
if n % 2 == 0 or n <= 1:
return False # Anything less than or equal to one is not prime.
for divisor in xrange(3, int(n ** 0.5) + 1, 2): # 3, 5, 7, ..., int(SQRT(n)) + 1
if not n % divisor:
return False # NOT divisible by the divisor.
return True # Must be prime.
def get_a_prime():
prime_found = False
while not prime_found:
a = random_odd_between(6 * 10 ^ 9, 10 ^ 10)
prime_found = check_if_prime(a)
return a
Testing
primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101]
# Ensure all primes above are prime per our function.
>>> all(check_if_prime(n) for n in primes)
True
# Ensure all numbers in range 0-101 that is not identified as a prime is not a prime.
>>> any(check_if_prime(n) for n in xrange(102) if n not in primes)
False

Related

user defined function to count the occurrences a randomly generated list contains an integer starting with n

New to python and programming in general. I've written a function to generate a random list with no duplicates and numerous other callable functions that calculate something from that list(e.g. median, average, evens, odds, %n, etc.)
Problem:
Write a function to count the number of integers that begin with 1 and
Write a function to count the number of integers that end with 1
My code is
import random
def fill(nx, x, y):
lx = []
j = 0
while (j < nx):
randNum = random.randint(x, y)
if randNum not in lx:
lx.append(randNum)
j = j + 1
return lx
def digit1x(lx):
#some kind of count
cnt_1x = 0
#loop to iterate lx?
for i in lx: # not sure what to really do from here
if i ==
return 0
def digitx1(lx):
# same problem
return 0
def sum(lx):
s=0
for i in lx:
s+=i
return s
Calling the Functions
n = 25
a = 10
b = 60
myList = fill(n, a, b)
print(myList)
Output
[32, 27, 57, 17, 14, 55, 29, 42, 23, 12, 11, 47, 60, 41, 31, 20,
21, 26, 56, 35, 30, 44, 54, 10, 50]
Sum == 844
Minimum 10
Maximum == 60
Average == 33.76
Expected
x = digit1x(myList)
print("10 the number of integers that start with the digit 1 == ", x)
output: "...==" 5
y = digitx1(myList)
print("11 the number of integers that end with the digit 1 == ", y)
output:"...==" 4
def digit1x(lx):
return len([i for i in lx if str(i)[0]=='1'])
def digitx1(lx):
return len([i for i in lx if str(i)[-1]=='1'])
and if you are short on memory and processing resources:
def digit1x(lx):
return sum(1 for i in lx if i%10==1])
def digitx1(lx):
return sum(1 for i in lx if i/(10**int(math.log(i,10)))==1)
In this case, you are avoiding the string conversion and use only some fast calculations. You also are using a generator for the sum, and hence no extra list is created.
This should do the job
def count1(lx):
return len([i for i in lx if i%10==1])

Find integer with most divisor from list of integers

I am trying to write a function to find integer with most divisor from a list, but i am getting the wrong output. This is how my function looks.
def find_my_integer_divisor(mylist):
def find_divisor(k):
count =0
for i in range (1,k+1):
if k%i==0:
count +=1
return count
A=mylist[0]
for x in mylist [0:]:
A=find_divisor(x)
return A
It returns the count of the last entry in mylist. I know I have to compare the value counts from each entry and returns the integer with the most count but don't know how to do it.
This should work:
def find_divisor(k):
count =0
for i in range (1,k+1):
if k%i==0:
count +=1
return count
def find_my_integer_divisor(mylist):
return max(mylist, key=find_divisor)
Instead of actually finding all the proper factors, we can much more efficiently calculate the number possible by doing a prime factorization.
For example,
288 == 2**5 * 3**2
and the number of proper factors is
(5 + 1) * (2 + 1) - 1
^ ^ ^
number number omit one case:
of twos of threes 5 2s and 2 3s == 288,
used in used in which is not a proper
factor, factor factor of itself
0..5
(six possible
values)
To do a prime factorization, we need to start by generating primes:
def primes(known_primes=[7, 11, 13, 17, 19, 23, 29]):
"""
Generate every prime number in ascending order
"""
# 2, 3, 5 wheel
yield from (2, 3, 5)
yield from known_primes
# The first time the generator runs, known_primes
# contains all primes such that 5 < p < 2 * 3 * 5
# After each wheel cycle the list of known primes
# will be added to.
# We need to figure out where to continue from,
# which is the next multiple of 30 higher than
# the last known_prime:
base = 30 * (known_primes[-1] // 30 + 1)
new_primes = []
while True:
# offs is chosen so 30*i + offs cannot be a multiple of 2, 3, or 5
for offs in (1, 7, 11, 13, 17, 19, 23, 29):
k = base + offs # next prime candidate
for p in known_primes:
if not k % p:
# found a factor - not prime
break
elif p*p > k:
# no smaller prime factors - found a new prime
new_primes.append(k)
break
if new_primes:
yield from new_primes
known_primes.extend(new_primes)
new_primes = []
base += 30
which can be tested like
from itertools import islice
print(list(islice(primes(), 500)))
giving
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, ...
Now that we have primes, we can count the occurrences of each prime factor like so:
def prime_factor_count(n):
"""
Factorize n and yield (factor, count) for each distinct factor
"""
if n < 2:
return
else:
for p in primes():
count = 0
while not n % p:
count += 1
n //= p
if count:
yield (p, count)
if n == 1:
# number is fully factored
break
elif p*p > n:
# no smaller prime factors - remainder is prime
yield (n, 1)
break
which we can test like
print(list(prime_factor_count(288))) # => [(2, 5), (3, 2)]
which you should recognize from above, 288 == 2**5 * 3**2. Now we can
def num_proper_factors(n):
total_factors = 1
for factor, count in prime_factor_count(n):
total_factors *= (count + 1)
return total_factors - 1
which tests like
print(num_proper_factors(288)) # => 17
and finally,
def num_with_most_divisors(lst):
return max(lst, key=num_proper_factors)
QED.
short answer: use max with a key function like your's find_divisor as show by #rofls.
Long answer: in each iteration you need to compare yours previous value with the current value in your list, if the current value have a bigger count of divisor change A otherwise don't, the problem in your code is that you don't do this check. You can do something like this
def max_divisor_count(my_list):
result = my_list[0]
for n in my_list[1:]: # start in 1 because 0 is already in result
if find_divisor(n) > find_divisor(result):
result = n
return result
and this is more or less the same that the max with key-function solution does.
Of course this can be improved a little more to avoid repeat calculations like this
def max_divisor_count(my_list):
result = my_list[0]
div_count = find_divisor(result)
for n in my_list[1:]: # start in position 1 because 0 is already in result
if result != n:
temp = find_divisor(n)
if temp > div_count:
result = n
div_count = temp
return result
This is a generator expression alternative. Note I use itertools.tee to create 2 instances of the generator. The first is to calculate the max, the second to feed enumerate.
The below example also demonstrates how you can use a list comprehension to return all integers with the maximum number of divisors.
from itertools import tee
lst = [1, 2, 3, 6, 8, 10, 14]
gen1, gen2 = tee(sum(k%i==0 for i in range(1, k+1)) for k in lst)
divmax = max(gen1)
[lst[i] for i, j in enumerate(gen2) if j == divmax]
# [6, 8, 10, 14]

Basic prime number generator in Python

Just wanted some feedback on my prime number generator. e.g. is it ok, does it use to much resources etc. It uses no libraries, it's fairly simple, and it is a reflection of my current state of programming skills, so don't hold back as I want to learn.
def prime_gen(n):
primes = [2]
a = 2
while a < n:
counter = 0
for i in primes:
if a % i == 0:
counter += 1
if counter == 0:
primes.append(a)
else:
counter = 0
a = a + 1
print primes
There are a few optimizations thar are common:
Example:
def prime(x):
if x in [0, 1]:
return False
if x == 2:
return True
for n in xrange(3, int(x ** 0.5 + 1)):
if x % n == 0:
return False
return True
Cover the base cases
Only iterate up to the square root of n
The above example doesn't generate prime numbers but tests them. You could adapt the same optimizations to your code :)
One of the more efficient algorithms I've found written in Python is found in the following question ans answer (using a sieve):
Simple Prime Generator in Python
My own adaptation of the sieve algorithm:
from itertools import islice
def primes():
if hasattr(primes, "D"):
D = primes.D
else:
primes.D = D = {}
def sieve():
q = 2
while True:
if q not in D:
yield q
D[q * q] = [q]
else:
for p in D[q]:
D.setdefault(p + q, []).append(p)
del D[q]
q += 1
return sieve()
print list(islice(primes(), 0, 1000000))
On my hardware I can generate the first million primes pretty quickly (given that this is written in Python):
prologic#daisy
Thu Apr 23 12:58:37
~/work/euler
$ time python foo.py > primes.txt
real 0m19.664s
user 0m19.453s
sys 0m0.241s
prologic#daisy
Thu Apr 23 12:59:01
~/work/euler
$ du -h primes.txt
8.9M primes.txt
Here is the standard method of generating primes adapted from the C# version at: Most Elegant Way to Generate Prime Number
def prime_gen(n):
primes = [2]
# start at 3 because 2 is already in the list
nextPrime = 3
while nextPrime < n:
isPrime = True
i = 0
# the optimization here is that you're checking from
# the number in the prime list to the square root of
# the number you're testing for primality
squareRoot = int(nextPrime ** .5)
while primes[i] <= squareRoot:
if nextPrime % primes[i] == 0:
isPrime = False
i += 1
if isPrime:
primes.append(nextPrime)
# only checking for odd numbers so add 2
nextPrime += 2
print primes
You start from this:
def prime_gen(n):
primes = [2]
a = 2
while a < n:
counter = 0
for i in primes:
if a % i == 0:
counter += 1
if counter == 0:
primes.append(a)
else:
counter = 0
a = a + 1
print primes
do you really need the else branch? No.
def prime_gen(n):
primes = [2]
a = 2
while a < n:
counter = 0
for i in primes:
if a % i == 0:
counter += 1
if counter == 0:
primes.append(a)
a = a + 1
print primes
Do you need the counter? No!
def prime_gen(n):
primes = [2]
a = 2
while a < n:
for i in primes:
if a % i == 0:
primes.append(a)
break
a = a + 1
print primes
Do you need to check for i larger that sqrt(a)? No.
def prime_gen(n):
primes = [2]
a = 3
while a < n:
sqrta = sqrt(a+1)
for i in primes:
if i >= sqrta:
break
if a % i == 0:
primes.append(a)
break
a = a + 1
print primes
Do you really want to manually increase a?
def prime_gen(n):
primes = [2]
for a in range(3,n):
sqrta = sqrt(a+1)
for i in primes:
if i >= sqrta:
break
if a % i == 0:
primes.append(a)
break
This is some basic refactoring that should automatically flow out of your fingers.
Then you test the refactored code, see that it is buggy and fix it:
def prime_gen(n):
primes = [2]
for a in range(3,n):
sqrta = sqrt(a+1)
isPrime = True
for i in primes:
if i >= sqrta:
break
if a % i == 0:
isPrime = False
break
if(isPrime):
primes.append(a)
return primes
And finally you get rid of the isPrime flag:
def prime_gen(n):
primes = [2]
for a in range(3,n):
sqrta = sqrt(a+1)
for i in primes:
if i >= sqrta:
primes.append(a)
break
if a % i == 0:
break
return primes
now you believe you're done. Then suddenly a friend of yours point out that for a even you are checking i >= sqrta for no reason. (Similarly for a mod 3 == 0 numbers, but then branch-prediction comes in help.)
Your friend suggest you to check a % i == 0 before:
def prime_gen(n):
primes = [2]
for a in range(3,n):
sqrta = sqrt(a+1)
for i in primes:
if a % i == 0:
break
if i >= sqrta:
primes.append(a)
break
return primes
now you're done and grateful to your brillant friend!
You can use Python yield statement to generate one item at the time. Son instead of get all items at once you will iterate over generator and get one item at the time. This minimizes your resources.
Here an example:
from math import sqrt
from typing import Generator
def gen(num: int) -> Generator[int, None, None]:
if 2 <= num:
yield 2
yield from (
i
for i in range(3, num + 1, 2)
if all(i % x != 0 for x in range(3, int(sqrt(i) + 1)))
)
for x in gen(100):
print(x, end=", ")
Output:
2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97,
I made improvements on the solution proposed my jimifiki
import math #for finding the sqare root of the candidate number
def primes(n):
test = [3] #list of primes new candidates are tested against
found = [5] #list of found primes, which are not being tested against
c = 5 #candidate number, starting at five
while c < n: #upper bound, largest candidate will be this or 1 bigger
p = True #notes the possibility of c to be prime
c += 2 #increase candidate by 2, avoiding all even numbers
for a in test: #for each item in test
if c % a == 0: #check if candidate is divisible
p = False #since divisible cannot be prime
break #since divisible no need to continue checking
if p: #true only if not divisible
if found[0] > math.sqrt(c): #is samallest in found > sqrt of c
found.append(c) #if so c is a prime, add it to the list
else: #if not, it's equal and we need to start checking for it
test.append(found.pop(0)) #move pos 0 of found to last in test
return([2] + test + found) #after reaching limit returns 2 and both lists
The biggest improvement is not checking for even numbers and checking the square root only if the number is not divisible, the latter really adds up when numbers get bigger. The reason we don't need to check for the square root is, that the test list only contains numbers smaller than the square root. This is because we add the next number only when we get to the first non-prime not divisible by any of the numbers in test. This number is always the square of the next biggest prime which is also the smallest number in found. The use of the boolean "p" feels kind of spaghetty to me so there might be room for improvement.
Here's a pretty efficient prime number generator that I wrote a while back that uses the Sieve of Eratosthenes:
#!/usr/bin/env python2.7
def primeslt(n):
"""Finds all primes less than n"""
if n < 3:
return []
A = [True] * n
A[0], A[1] = False, False
for i in range(2, int(n**0.5)+1):
if A[i]:
j = i**2
while j < n:
A[j] = False
j += i
return [num for num in xrange(n) if A[num]]
def main():
i = ''
while not i.isdigit():
i = raw_input('Find all prime numbers less than... ')
print primeslt(int(i))
if __name__ == '__main__':
main()
The Wikipedia article (linked above) explains how it works better than I could, so I'm just going to recommend that you read that.
I have some optimizations for the first code which can be used when the argument is negative:
def is_prime(x):
if x <=1:
return False
else:
for n in xrange(2, int(x ** 0.5 + 1)):
if x % n == 0:
return False
return True
print is_prime(-3)
Being Python, it usually better to return a generator that will return an infinite sequence of primes rather than a list.
ActiveState has a list of older Sieve of Eratosthenes recipes
Here is one of them updated to Python 2.7 using itertools count with a step argument which did not exist when the original recipe was written:
import itertools as it
def sieve():
""" Generate an infinite sequence of prime numbers.
"""
yield 2
D = {}
for q in it.count(3, 2): # start at 3 and step by odds
p = D.pop(q, 0)
if p:
x = q + p
while x in D: x += p
D[x] = p # new composite found. Mark that
else:
yield q # q is a new prime since no composite was found
D[q*q] = 2*q
Since it is a generator, it is much more memory efficient than generating an entire list. Since it locates composite, it is computationally efficient as well.
Run this:
>>> g=sieve()
Then each subsequent call returns the next prime:
>>> next(g)
2
>>> next(g)
3
# etc
You can then get a list between boundaries (i.e., the Xth prime from the first to the X+Y prime...) by using islice:
>>> tgt=0
>>> tgt, list(it.islice(sieve(), tgt, tgt+10))
(0, [2, 3, 5, 7, 11, 13, 17, 19, 23, 29])
>>> tgt=1000000
>>> tgt, list(it.islice(sieve(), tgt, tgt+10))
(1000000, [15485867, 15485917, 15485927, 15485933, 15485941, 15485959, 15485989, 15485993, 15486013, 15486041])
To Get the 100th prime number:
import itertools
n=100
x = (i for i in itertools.count(1) if all([i%d for d in xrange(2,i)]))
print list(itertools.islice(x,n-1,n))[0]
To get prime numbers till 100
import itertools
n=100
x = (i for i in xrange(1,n) if all([i%d for d in xrange(2,i)]))
for n in x:
print n
you can do it this way also to get the primes in a dictionary in python
def is_prime(a):
count = 0
counts = 0
k = dict()
for i in range(2, a - 1):
k[count] = a % i
count += 1
for j in range(len(k)):
if k[j] == 0:
counts += 1
if counts == 0:
return True
else:
return False
def find_prime(f, g):
prime = dict()
count = 0
for i in range(int(f), int(g)):
if is_prime(i) is True:
prime[count] = i
count += 1
return prime
a = find_prime(20,110)
print(a)
{0: 23, 1: 29, 2: 31, 3: 37, 4: 41, 5: 43, 6: 47, 7: 53, 8: 59, 9: 61, 10: 67, 11:
71, 12: 73, 13: 79, 14: 83, 15: 89, 16: 97, 17: 101, 18: 103, 19: 107, 20: 109}

python prime numbers Sieve of Eratosthenes

Hi can anyone tell me how to implement Sieve of Eratosthenes within this code to make it fast? Help will be really appreciated if you can complete it with sieve. I am really having trouble doing this in this particular code.
#!/usr/bin/env python
import sys
T=10 #no of test cases
t=open(sys.argv[1],'r').readlines()
import math
def is_prime(n):
if n == 2:
return True
if n%2 == 0 or n <= 1:
return False
sqr = int(math.sqrt(n)) + 1
for divisor in range(3, sqr, 2):
if n%divisor == 0:
return False
return True
#first line of each test case
a=[1,4,7,10,13,16,19,22,25,28]
count=0
for i in a:
b=t[i].split(" ")
c=b[1].split("\n")[0]
b=b[0]
for k in xrange(int(b)):
d=t[i+1].split(" ")
e=t[i+2].split(" ")
for g in d:
for j in e:
try:
sum=int(g)+int(j)
p=is_prime(sum)
if p==True:
count+=1
print count
else:
pass
except:
try:
g=g.strip("\n")
sum=int(g)+int(j)
p=is_prime(sum)
if p==True:
count+=1
print count
else:
pass
except:
j=j.strip("\n")
sum=int(g)+int(j)
p=is_prime(sum)
if p==True:
count+=1
print count
else:
pass
print "Final count"+count
An old trick for speeding sieves in Python is to use fancy ;-) list slice notation, like below. This uses Python 3. Changes needed for Python 2 are noted in comments:
def sieve(n):
"Return all primes <= n."
np1 = n + 1
s = list(range(np1)) # leave off `list()` in Python 2
s[1] = 0
sqrtn = int(round(n**0.5))
for i in range(2, sqrtn + 1): # use `xrange()` in Python 2
if s[i]:
# next line: use `xrange()` in Python 2
s[i*i: np1: i] = [0] * len(range(i*i, np1, i))
return filter(None, s)
In Python 2 this returns a list; in Python 3 an iterator. Here under Python 3:
>>> list(sieve(20))
[2, 3, 5, 7, 11, 13, 17, 19]
>>> len(list(sieve(1000000)))
78498
Those both run in an eyeblink. Given that, here's how to build an is_prime function:
primes = set(sieve(the_max_integer_you_care_about))
def is_prime(n):
return n in primes
It's the set() part that makes it fast. Of course the function is so simple you'd probably want to write:
if n in primes:
directly instead of messing with:
if is_prime(n):
Both the original poster and the other solution posted here make the same mistake; if you use the modulo operator, or division in any form, your algorithm is trial division, not the Sieve of Eratosthenes, and will be far slower, O(n^2) instead of O(n log log n). Here is a simple Sieve of Eratosthenes in Python:
def primes(n): # sieve of eratosthenes
ps, sieve = [], [True] * (n + 1)
for p in range(2, n + 1):
if sieve[p]:
ps.append(p)
for i in range(p * p, n + 1, p):
sieve[i] = False
return ps
That should find all the primes less than a million in less than a second. If you're interested in programming with prime numbers, I modestly recommend this essay at my blog.
Fastest implementation I could think of
def sieve(maxNum):
yield 2
D, q = {}, 3
while q <= maxNum:
p = D.pop(q, 0)
if p:
x = q + p
while x in D: x += p
D[x] = p
else:
yield q
D[q*q] = 2*q
q += 2
raise StopIteration
Source: http://code.activestate.com/recipes/117119-sieve-of-eratosthenes/#c4
Replace this part
import math
def is_prime(n):
if n == 2:
return True
if n%2 == 0 or n <= 1:
return False
sqr = int(math.sqrt(n)) + 1
for divisor in range(3, sqr, 2):
if n%divisor == 0:
return False
return True
with
primes = [prime for prime in sieve(10000000)]
def is_prime(n):
return n in primes
Instead of 10000000 you can put whatever the maximum number till which you need prime numbers.
Here is a very fast generator with reduced memory usage.
def pgen(maxnum): # Sieve of Eratosthenes generator
yield 2
np_f = {}
for q in xrange(3, maxnum + 1, 2):
f = np_f.pop(q, None)
if f:
while f != np_f.setdefault(q+f, f):
q += f
else:
yield q
np = q*q
if np < maxnum: # does not add to dict beyond maxnum
np_f[np] = q+q
def is_prime(n):
return n in pgen(n)
>>> is_prime(541)
True
>>> is_prime(539)
False
>>> 83 in pgen(100)
True
>>> list(pgen(100)) # List prime numbers less than or equal to 100
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83,
89, 97]
Here is a simple generator using only addition that does not pre-allocate memory. The sieve is only as large as the dictionary of primes and memory use grows only as needed.
def pgen(maxnum): # Sieve of Eratosthenes generator
pnext, ps = 2, {}
while pnext <= maxnum:
for p in ps:
while ps[p] < pnext:
ps[p] += p
if ps[p] == pnext:
break
else:
ps[pnext] = pnext
yield pnext
pnext += 1
def is_prime(n):
return n in pgen(n)
>>> is_prime(117)
>>> is_prime(117)
False
>>> 83 in pgen(83)
True
>>> list(pgen(100)) # List prime numbers less than or equal to 100
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83,
89, 97]
This is a simple solution with sets.
Which is very fast in comparison with many of the list-algorithms.
Computation with sets is much faster because of the hash tables.
(What makes sets faster than lists in python?)
Greetings
----------------------------------
from math import *
def sievePrimes(n):
numbers = set()
numbers2 = set()
bound = round(sqrt(n))
for a in range(2, n+1):
numbers.add(a)
for i in range(2, n):
for b in range(1, bound):
if (i*(b+1)) in numbers2:
continue
numbers2.add(i*(b+1))
numbers = numbers - numbers2
print(sorted(numbers))
Simple Solution

Iterating until a function returns True a user defined number of times

I've written a function, isprime(n), that returns True if a number is prime and false if not.
I am able to loop the function a defined number of times; but I can't figure out how to iterate until it finds x number of primes. I feel as though I have a decent understanding of For and While loops, but am confused as to how one integrates boolean return values into loops. Here is my current code and error:
Error result:
input:100
Traceback (most recent call last):
File "euler7.py", line 25, in <module>
primeList += 1
TypeError: 'int' object is not iterable
And the code:
def isprime(n):
x = 2
while x < sqrt(n):
if n % x == 0:
return False
else:
x += 1
return True
userinput = int(raw_input('input:'))
primeList = []
primesFound = 0
while primesFound != userinput:
i = 2
if isprime(i):
primeList.append(i)
primeList += 1
i += 1
else:
i += 1
EDIT (including the updated and functioning code):
from math import sqrt
def isprime(n):
x = 2
while x < (sqrt(n) + 1):
if n % x == 0:
return False
else:
x += 1
return True
userinput = int(raw_input('input:'))
primeList = []
primeList.append(2)
i = 2
while len(primeList) != userinput:
if isprime(i):
primeList.append(i)
i += 1
else:
i += 1
print 'result:', primeList[-1]
This line:
primeList += 1
Should be:
primesFound += 1
You cannot add and int to a python list. You should do primesFound += 1 to achieve your desired result.
Plus, your isprime function is wrong. It will return True for 9. You should do while x < sqrt(n) + 1 for the while loop of your isprime function.
So you should have:
def isprime(n):
x=2
while x < sqrt(n) +1:
if n % x == 0:
return False
else:
x += 1
return True
As others have pointed out:
You should increment primesFound, not primeList.
The isprime() function has a bug -- and returns True for 9. You need sqrt(n) + 1.
In addition:
You need to initialize i outside the while loop; otherwise, you simply build up a list of 2's.
There is no need for primesFound. Just check len(primeList).
And my pet peeve:
Command-line programs should resort to interactive user input only in special circumstances. Where possible, take parameters as command-line arguments or options. For example: userinput = int(sys.argv[1]).
To get n numbers that satisfy some condition, you could use itertools.islice() function and a generator expression:
from itertools import count, islice
n = int(raw_input('number of primes:'))
primes = list(islice((p for p in count(2) if isprime(p)), n))
where (p for p in count(2) if isprime(p)) is a generator expression that produces prime numbers indefinitely (it could also be written as itertools.ifilter(isprime, count(2))).
You could use Sieve of Eratosthenes algorithm, to get a more efficient solution:
def primes_upto(limit):
"""Yield prime numbers less than `limit`."""
isprime = [True] * limit
for n in xrange(2, limit):
if isprime[n]:
yield n
for m in xrange(n*n, limit, n): # mark multiples of n as composites
isprime[m] = False
print list(primes_upto(60))
# -> [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59]
See Fastest way to list all primes below N in python.
Note: there are about limit / (log(limit) - 1) prime numbers less than limit.
You could also use an infinite prime number generator such as gen_primes(), to get the first n primes numbers:
primes = list(islice(gen_primes(), n))
See How to implement an efficient infinite generator of prime numbers in Python?
def is_prime(n):
x=2
while x < sqrt(n) +1:
if n % x == 0:
return False
break
else:
x += 1
return True

Categories