How can I display the first 25 integer prime numbers in the given interval? I can't find a way to limit it to 25 integer prime numbers.
minimum = 1000000000
maximum = 9999999999
print ("The first 25 10-digit prime numbers are:")
for num in range (minimum, maximum + 1):
if num > 1:
for i in range (2, num):
if (num % i) == 0:
break
else:
print(num)
you have to use a counter to store how many prime numbers you've found.
minimum = 1000000000
maximum = 9999999999
print ("The first 25 10-digit prime numbers are:")
counter = 0
for num in range (minimum, maximum + 1):
if counter == 25: break
if num > 3:
for i in range (2, int(num/2)): # for optimization
if (num % i) == 0:
break
else:
counter += 1
print(counter,num)
else:
counter += 1
print(counter,num)
here is the more optimized code with the hints in the comments:
import math
minimum = 1000000000
maximum = 9999999999
print ("The first 25 10-digit prime numbers are:")
counter = 0
for num in range (minimum, maximum + 1):
if counter == 25: break
if num > 3:
if (num % 2) != 0:
for i in range (3, math.ceil(math.sqrt(num)), 2):
if (num % i) == 0:
break
else:
counter += 1
print(counter,num)
else:
counter += 1
print(counter,num)
If nothing else, you could use a variable to keep track of how many you've printed and break from the outer loop when that hits 25.
Here's an example, added to your code.
minimum = 1000000000
maximum = 9999999999
printed = 0
print ("The first 25 10-digit prime numbers are:")
for num in range (minimum, maximum + 1):
if num > 1:
for i in range (2, num):
if (num % i) == 0:
break
else:
print(num)
printed+= 1
if printed >= 25:
break
You can try this.
from sympy import isprime
i=1
count = 0
while count < 25:
if isprime(i):
print(i)
count += 1
i += 1
Related
I have to count the sum of all prime numbers that are less than 1000 and do not contain the digit 3.
My code:
def primes_sum(lower, upper):
total = 0
for num in range(lower, upper + 1):
if not num % 3 and num % 10:
continue
elif num > 1:
for i in range(2, num):
if num % i == 0:
break
else:
total += num
return total
total_value = primes_sum(0, 1000)
print(total_value)
But still I don't have right result
def primes_sum(lower, upper):
"""Assume upper>=lower>2"""
primes = [2]
answer = 2
for num in range(lower, upper+1):
if any(num%p==0 for p in primes): continue # not a prime
primes.append(num)
if '3' in str(num): continue
answer += num
return answer
The issue in your code was that you were checking for num%3, which checks whether num is divisible by 3, not whether it contains a 3.
Write a function (say, sum_primes) in python3 that takes in a number and returns the sum of all primes less than or equal to that number.
I tried like:
def sum_primes(num):
total = 0
for i in range(2, num+1):
if num%i == 0:
break
else:
total += i
return total
sum_primes(11)
but doesn't work.
you can try this way:
#Take the input from user
upto = int(input("Find sum of prime numbers up to : "))
sum = 0
for num in range(2, upto + 1):
i = 2
for i in range(2, num):
if (int(num % i) == 0):
i = num
break;
#If the number is prime then add it.
if i is not num:
sum += num
print("\nSum of all prime numbers upto", upto, ":", sum)
I am a Python beginner.
I got a prime number within the specified range. However, I couldn't convert it to a list.
I also tried converting it to a string and back to a list.
import random
lower = int(input("Enter lower range: "))
upper = int(input("Enter upper range: "))
for num in range(lower,upper + 1):
if num > 1:
for i in range(2, num):
if (num % i) == 0:
break
else:
print(num)
Start with an empty list, and every time you find a prime number, append it to the list. Then you can print the list at the end:
nums = []
for num in range(lower,upper + 1):
if num > 1:
for i in range(2, num):
if (num % i) == 0:
break
else:
nums.append(num)
print(nums)
Note that you only need to iterate up the square root of num, any factor beyond that can only have another factor which is less than it:
from math import sqrt
nums = []
for num in range(lower,upper + 1):
if num > 1:
for i in range(2, int(sqrt(num))):
if (num % i) == 0:
break
else:
nums.append(num)
print(nums)
I just started learning python. I'm trying to write a program that prints prime no. but it also printing no. ending with 5.
n = 2
while n >= 2:
if n == 2:
print(n)
n += 1
while n >= 3:
if n == 3:
print(n)
n += 2
for i in range (3,n):
if n % i != 0:
print(n)
n += 2
Here is an algorithm for deciding primes:
def prime(iterations):
primes = [2]
x = math.floor(math.sqrt(iterations)) + 1
for i in range(iterations):
if i > 2:
primes.append(i)
for i in range(x):
if i > 1:
j=2
while i*j <= iterations:
if i*j in primes:
primes.remove(i*j)
j += 1
print(primes)
explanations:
line 4: appends every num to primes
line 7 and line 3: finds numbers between 1 and iterations and removes all possible factors
line 13: cycling through factors
You can use below code for finding prime number.
num = int(input("Enter number: "))
if num > 1:
for i in range(2, num):
if (num % i) == 0:
print(num, " is not a prime number")
break
else:
print(num, " is a prime number")
else:
print(num, " is not a prime number")
prime number always greater than 1 so you have check for it also.
x=int(input("Give your No: "))
for k in range(2,x):
if ((x%k)==0):
print('Not prime no')
break
else:
print("Prime no")
I'm a beginning programmer in python and have a question about my code I'm writing:
number = int(input("Enter a random number: "))
for num in range(1, number + 1) :
for i in range(2, num) :
if (num % i) == 0 :
break
else :
print(num)
break
When I run this program I also get 9, 15 21 as output. But these are not prime numbers.
What is wrong with my code?
Thanks!
With if (num % i) == 0: you go to the else block for every num, which is not a multiply of 2, as you start i with 2, thus printing num. I got all odd numbers printed with your code.
You can use something like this:
number = int(input("Enter a random number: "))
for num in range(1, number + 1):
prime = True
for i in range(2, num):
if (num % i) == 0:
prime = False
break
if prime:
print(num)
It sets prime to False when it encounters a divisor without rest.
the trouble is in your else: statement which has to run outside of the first loop. I recommend the following:
def isprime(num):
for i in range(2, num):
if (num % i) == 0:
return False
return True
for num in range(1, number + 1) :
if isprime(num):
print num
Your problem
The else statement that you put inside the for loop means that if that number(num) is divisible by this current no represented by i, return true considering it as a prime which IS NOT CORRECT.
Suggestion
Your outer loop starts with 1 which should change to 2, as 1 is not a prime number
Solution
def fun(number):
#Not counting 1 in the sequence as its not prime
for num in range(2, number + 1) :
isPrime = True
for i in range(2, num) :
if (num % i) == 0 :
isPrime = False
break
if isPrime:
print num
fun(10)
Output
1
2
3
5
7
I am assuming the random number is the range you want the numbers to be within.
I found that the variable i is always equal to 2 in your code.This destroys the purpose of having a second for loop
Prime numbers are numbers that cannot be divisible by 2, 3 or 7, excluding 2, 3 or 7!
With this knowledge I adapted your code to show the true prime numbers.
solution
number = int(input("Enter a random number: "))
for num in range(2,number +1) :
printnum = True
if num == 2:
printnum = True
elif num == 3:
printnum = True
elif num == 7:
printnum = True
elif num % 2 == 0:
printnum = False
elif num % 3 == 0:
printnum = False
elif num % 7 == 0:
printnum = False
if printnum == True:
print(num)
This is my method to generate first ten prime numbers with no imports, just simple code with components we are learning on college. I save those numbers into list.
def main():
listaPrim = []
broj = 0
while len(listaPrim) != 10:
broj+= 1
brojac = 0
for i in range(1,100):
if broj % i == 0:
brojac += 1
if brojac == 2:
listaPrim.append(broj)
print(listaPrim)
if __name__=='__main__':
main()