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 1 year ago.
Improve this question
n = int(input('Enter number '))
def prime(n):
for i in range (2,int(n-1)):
if (n%i)==0:
print('no is not prime')
break
else:
print('no is prime')
break
prime(n)
this will display prime numbers from 900 to 1000
lower = 900
upper = 1000
print("Prime numbers between", lower, "and", upper, "are:")
for num in range(lower, upper + 1):
# all prime numbers are greater than 1
if num > 1:
for i in range(2, num):
if (num % i) == 0:
break
else:
print(num)
Related
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 1 year ago.
Improve this question
I don't know what is wrong. I am trying to find largest prime factor
import math
def getfactor(num):
factors = []
for i in range(2,int((math.sqrt(num)))+1):
if num % i == 0:
factors.append(i)
return factors
def determineprime(num):
factor = []
for i in range(2,num + 1):
if num % i == 0:
factor.append(i)
if len(factor) == 1:
return True
else:
return False
factors = getfactor(600851475143)
primes = []
print(factors)
for i in factors:
determineprime(i)
if i:
primes.append(i)
print(primes[-1])
Change:
determineprime(i)
if i:
primes.append(i)
to:
if determineprime(i):
primes.append(i)
The original version was calling determineprime(i), discarding the result, and always appending i to primes. The corrected version will only append i to primes if determineprime(i) returns true.
Update: As Mark Tolonen pointed out, getfactor will return an empty list if its argument is prime. In that case, the number itself should be returned as the sole element of the list. It can just check to see if the list is empty before returning, and if so, append the number itself, since it must be prime in that case.
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 2 years ago.
Improve this question
I am new to Python and coding. This is what I have so far, but can't get it to work.
def halve_to_2( num ):
while True:
num = num /2
if num < 2:
else:
if num <=0:
print ("-1")
return num
k = int(input("Enter a number"))
print(halve_to_2(k))
def halve_to_2( num ):
while True:
if num <= 0:
print("-1")
return -1
else:
num = num / 2
k = int(input("Enter a number"))
print(halve_to_2(k))
def divisibleByTwo( num) :
if num <=0 :
return - 1
else :
divisibleByTwo (num/2)
Its called recursion. The break point is the return - 1 for this recursive loop
def divisibleByTwo( num) :
print ( num)
if int(num) > 0 :
divisibleByTwo (num/2)
return num/2
This one print the values that fall within integers not scientific decimals
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
#program of prime number from 2 to 100
for i in range (2,100):
count = 0
for j in range(1, i+1):
if i % j = 0:
count = count + 1
if count == 2:
print(i)
First of all, your code seems to not work due to a syntax error(might be a typo).
if conditions in python, requires double equals (==).
if i % j == 0:
In the question you posted, the count variable acts as a flag or counter variable.
A prime number is a number which can only be perfectly divided, by 1 or the number itself. (eg: 11 can be divided by 1 and 11 only, therefore a prime number)
So the counter variable i.e. count holds the number of times that a number(2-200) gets perfectly divided(i.e. (%) modulo operation results in value 0). If the value of count is 2 then we can confirm that it is a prime number since a prime number can only be divided with 1 and the number itself (hence count=2). And if the count == 2 after all the possible divisions happening in the j loop (2nd for loop), we can conclude that it is a prime number.
for i in range (2,200):
count = 0
for j in range(1, i+1):
if i % j == 0:
count += 1
if count == 2:
print(i)
count variable is being used to ensure if the particular number i is being divisible by any other number other than 1. Because if it does, then it is not a prime number. The moment it becomes 2, it means that number is divisible by 1 and some other number less than i proving it be a non-prime number.
Run this to get primes from 100, you can change the 100 to whatever you want
for num in range(100):
if num > 1:
for i in range(2,num):
if (num % i) == 0:
print(num,"is not a prime number")
print(i,"times",num//i,"is",num)
break
else:
print(num,"is a prime number")
else:
print(num,"is not a prime number")
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
Task is : input two numbers that is a diapasone . Then count a quantity of simple numbers into this diapasone.
Please correct my python code below
import math
count = (int, input().split())
for i in count:
n = int(input())
for j in range(2, int(math.sqrt(n)) + 1):
if n % j == 0:
break
print(count)
If you want to count the prime numbers in the closed interval [j, k] (which is given with input()), then you could use
import math
interval = tuple(map(int, input().split()))
count = 0
for n in range(max(2, interval[0]), interval[1]+1):
for j in range(2, int(math.sqrt(n)) + 1):
if n % j == 0:
break
else:
count += 1
print(count)
Explanation:
use map to apply int on each string
use count as a counter variable
use range as an iterator over integers in given interval
start prime number tests with 2, even if lower bound is smaller
count up only if no divisor is found (else clause of for loop)
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 5 years ago.
Improve this question
Why inside for loop is written upper + 1 and not just upper. If I change upper+1 with upper and run the code, I will have the same result!
for num in range(lower,**upper + 1)**
# Python program to display all the prime numbers within an interval
lower = int(input("Enter lower range: "))
upper = int(input("Enter upper range: "))
print("Prime numbers between",lower,"and",upper,"are:")
for num in range(lower,upper + 1):
# prime numbers are greater than 1
if num > 1:
for i in range(2,num):
if (num % i) == 0:
break
else:
print(num)
It's because range(x,y) stops at y-1. For example:
for i in range(2,5):
print(i)
will give you:
2
3
4
So if you want to check upper as well, you should add 1, so upper+1. Your code returns the same results since upper is not prime, so even if upper is checked (with upper+1 in range), upper will not be printed.