finding prime numbers greater than 10^12 [closed] - python

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
What is the sum of all prime numbers between 1,000,000,000,000 and 1,000,000,100,000?
this works out but is very slow.I need to optimize it.I am new to python.
3614000181007876 is the right answer
A=10 ** 6
N=A+1
B=10 ** 5
prime=[]
sum=0
for i in range(0,N):
prime.append(0)
for i in range(2,N):
if(prime[i]==1):
continue
for j in range(i*i,N,i):
prime[j]=1
for i in range((A ** 2)+1,(A ** 2)+B,2):
for j in range(2,A):
c=0
if(prime[j]==1):
continue
if(i%j==0):
c=c+1
if(c>0):
break
if(c==0):
#print(i)
sum=sum+i
print(sum)

Not the most efficient way, but gets the right result (hopefully 3614000181007876) in 2 seconds on my box:
def isPrime(n):
d = n - 1
s = 0
while not d & 1:
s += 1
d >>= 1
for a in (2, 13, 23, 1662803):
if pow(a, d, n) != 1 and all(pow(a, (1 << r) * d, n) != n - 1 for r in range(0, s)):
return False
return True
print(sum(x for x in range(1000000000001, 1000000100000, 2) if isPrime(x)))

Related

Fibonacci sequence calculation in Python [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 months ago.
Improve this question
I tried to do a function that builds the Fibonacci series but when I try to check the calculation comes out wrong
def fibo(n):
i=1
j=1
for n in range(1,n):
j=j+i
i=j+i
return i+j
n=input('Enter number:')
print(fibo(int(n)))
i, j = 1, 1
for _ in range(n):
j = j + i
i = j + i
This is not the fibonacci sequence. Instead, try:
i, j = 1, 1
for _ in range(n):
i, j = j, i+j

How do I check divisibility in python? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I am writing a python script right now to check if a number a Wilson Prime, but it says I have a syntax error on line 9.
I've tried both the mod function with x%y == 0 and x/y == int, but both of them have given me a syntax error.
n = int(input("Type a natural number here"))
fact = 1
for i in range(1,n):
fact = fact * i
finalfact = fact + 1
if finalfact % n == 0
print(n, "is a prime number!")
if (finalfact/n) % n == 0
print(n, "is also a Wilson Prime!")
I'm trying to make it check if (n-1)!+1 is divisible by n (which is a way to find prime numbers) and to check if (n-1)!+1 is divisible by n^2 (Wilson Primes), but it gives me a syntax error on line 9 and nothing happens.
You are missing : at the end of if statements.
Change:
if finalfact % n == 0
To:
if finalfact % n == 0:
And:
if (finalfact/n) % n == 0
To:
if (finalfact/n) % n == 0:
Need to indent the code properly:
Change:
for i in range(1,n):
fact = fact * i
To:
for i in range(1,n):
fact = fact * i
Also:
finalfact / n # 5 / 2 = 2.5
Should be:
finalfact // n # 5 // 2 = 2

How do I print the total number of iterations of the inner loop that were completed at the end of the program? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
This is what I'm assigned to do:
I don't understand how to do the second half (Perfect number) of the rubric.
This is what I have so far:
def sumMultiple(num):
sum = 0
for i in range(1, num//2+1):
if (num % i == 0):
sum += i
return sum
for in in range(1, 100000):
if(sumMultiple(i) == i):
print(i)
You can use a global variable that you iterate in the inner loop.
iterationCount = 0
def sumMultiple(num):
global iterationCount
sum = 0
for i in range(1, num//2+1):
if num % i == 0:
sum += i
iterationCount++
return sum
for i in range(1, 1000):
if sumMultiple(i) == i:
print(i)
print("Total iterations ", iterationCount)

simplify the python code with while loop and if condition [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 5 years ago.
Improve this question
I am totally new for python3. I need to write some easy process as following. But wondering if there is anyway I can simplify the following code?
def dosomething ( i ):
print(i);
n = 12
i = 1
while n > 0:
if i == 6:
i = 5
dosomething( i )
i += 1
n -= 1
It's not clear what you're trying to do with this example or if your constants are significant, but here's one approach that creates the same output a bit more simply:
def dosomething ( i ):
print(i);
n = 12
i = 1
for j in range(1, 6):
dosomething(j)
for k in range(n - 5):
dosomething(5)
In general, if the number of iterations is known in advance, you should express it with a for loop rather than a while loop.
this would work, simplifying the while loop into a for loop:
i = 1
for n in range(1, 13):
if i == 6:
i = 5
dosomething(i)
i += 1
if you are trying to avoid dosomething(6):
for n in range(1, 13):
if n != 6:
dosomething(n)

Fibonacci numbers integer taken [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have 2 questions about Fibonacci numbers.
1) The number of integers taken as an argument into the Fibonacci base functions? (in Turkish: Argüman olarak alınan onluk tabanda ki sayıyı Fibonacci tabanına dönüştüren işlev?)
2) The number given in the Fibonacci function converts integer base? (in Turkish: Fibonacci tabanında verilmiş sayıyı onluk tabana dönüştüren işlev?)
a, b = 0, 1
print (a)
print (b)
i = 2
while i<=50:
a,b = b, a+b
print (b)
i+=1
i can wrote that, but i want such as something like f(10) or f(5)
I'm sorry, my english isn't so good. Thank you.
Just move it all to a function and handle special cases.
def fib(n):
#TODO: handle invalid case(negative or non-int) and return
if(n == 0):
print(0)
return
if(n == 1):
print(1)
return
a, b = 0, 1
i = 2
#this will go up to the n-th number
while i<=n:
a,b = b, a+b
i+=1
print (b)
Then you can do this:
fib(1)
fib(0)
fib(6)
fib(33)
And you'll get:
1
0
8
3524578

Categories

Resources