Hi. I am trying to enter a number into this script and have it do several things. 1st I want it to find all of the prime numbers from the one that's entered by the user. I found some code online that does that (below). Then I want only every other prime in the list, which I've managed with "a=primeLis t[1::2]". I'm not sure how to go about the next part which will be to take the first number of a multi-digit prime and add it to the last number of that prime and do it for all primes in the list. Example:
Enter number to begin with:125
List of prime numbers from 2 to < 125:
[113, 107, 101, 89, 79, 71, 61, 53, 43, 37, 29, 19, 13, 7, 3]
1+3, 1+7, 1+1, 8+9, 7+9....etc (single digits get added to themselves so for 7,3 it would be 7+7, 3+3)
How do I go through my list of every other prime and get those particular numbers to add?
Enter number to begin with:125
List of prime numbers from 2 to < 125:
[113, 107, 101, 89, 79, 71, 61, 53, 43, 37, 29, 19, 13, 7, 3]
1+3, 1+7, 1+1, 8+9, 7+9....etc (single digits get added to themselves so for 7,3 it would be 7+7, 3+3)
How do I go through my list of every other prime and get those particular numbers to add?
Code:
def primes(n):
""" returns a list of prime numbers from 2 to < n """
if n < 2: return []
if n == 2: return [2]
# do only odd numbers starting at 3
s = range(3, n, 2)
# n**0.5 may be slightly faster than math.sqrt(n)
mroot = n ** 0.5
half = len(s)
i = 0
m = 3
while m <= mroot:
if s[i]:
j = (m * m - 3)//2
s[j] = 0
while j < half:
s[j] = 0
j += m
i = i + 1
m = 2 * i + 3
# make exception for 2
return [2]+[x for x in s if x]
num = input("Enter number you'd like to begin with:")
primeList = primes(num)
print "List of prime numbers from 2 to < %d:" % num
a=primeList[1::2]
print a
Comment