Testing divisibility of a list and appending if prime
up vote
1
down vote
favorite
I'm writing a short snippet for a class that is supposed to run through a given list of numbers and append any primes. Right now it is returning all numbers in the range though.
I've found examples online for how to do this, but wanted to try it myself but I've seem to hit a wall... Here is my code:
from random import randrange
from time import sleep
def prime():
user_num = eval(input("Input a number: "))
list_prime =
for i in range(2,user_num):
if (i % 2) == 1 and
(i % 3) == 1 and
(i % 4) == 1 and
(i % 5) == 1 and
(i % 6) == 1 and
(i % 7) == 1 and
(i % 8) == 1 and
(i % 9) == 1 or
i == 2:
list_prime.append(i)
if list_prime == '':
print('No prime numbers.')
if list_prime != '':
print('nPrime numbers from 1 to ' + str(user_num) + ': ' + str(list_prime))
sleep(1)
print('nClosing console in 60 seconds...')
sleep(60)
prime()
python-3.x primes
add a comment |
up vote
1
down vote
favorite
I'm writing a short snippet for a class that is supposed to run through a given list of numbers and append any primes. Right now it is returning all numbers in the range though.
I've found examples online for how to do this, but wanted to try it myself but I've seem to hit a wall... Here is my code:
from random import randrange
from time import sleep
def prime():
user_num = eval(input("Input a number: "))
list_prime =
for i in range(2,user_num):
if (i % 2) == 1 and
(i % 3) == 1 and
(i % 4) == 1 and
(i % 5) == 1 and
(i % 6) == 1 and
(i % 7) == 1 and
(i % 8) == 1 and
(i % 9) == 1 or
i == 2:
list_prime.append(i)
if list_prime == '':
print('No prime numbers.')
if list_prime != '':
print('nPrime numbers from 1 to ' + str(user_num) + ': ' + str(list_prime))
sleep(1)
print('nClosing console in 60 seconds...')
sleep(60)
prime()
python-3.x primes
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I'm writing a short snippet for a class that is supposed to run through a given list of numbers and append any primes. Right now it is returning all numbers in the range though.
I've found examples online for how to do this, but wanted to try it myself but I've seem to hit a wall... Here is my code:
from random import randrange
from time import sleep
def prime():
user_num = eval(input("Input a number: "))
list_prime =
for i in range(2,user_num):
if (i % 2) == 1 and
(i % 3) == 1 and
(i % 4) == 1 and
(i % 5) == 1 and
(i % 6) == 1 and
(i % 7) == 1 and
(i % 8) == 1 and
(i % 9) == 1 or
i == 2:
list_prime.append(i)
if list_prime == '':
print('No prime numbers.')
if list_prime != '':
print('nPrime numbers from 1 to ' + str(user_num) + ': ' + str(list_prime))
sleep(1)
print('nClosing console in 60 seconds...')
sleep(60)
prime()
python-3.x primes
I'm writing a short snippet for a class that is supposed to run through a given list of numbers and append any primes. Right now it is returning all numbers in the range though.
I've found examples online for how to do this, but wanted to try it myself but I've seem to hit a wall... Here is my code:
from random import randrange
from time import sleep
def prime():
user_num = eval(input("Input a number: "))
list_prime =
for i in range(2,user_num):
if (i % 2) == 1 and
(i % 3) == 1 and
(i % 4) == 1 and
(i % 5) == 1 and
(i % 6) == 1 and
(i % 7) == 1 and
(i % 8) == 1 and
(i % 9) == 1 or
i == 2:
list_prime.append(i)
if list_prime == '':
print('No prime numbers.')
if list_prime != '':
print('nPrime numbers from 1 to ' + str(user_num) + ': ' + str(list_prime))
sleep(1)
print('nClosing console in 60 seconds...')
sleep(60)
prime()
python-3.x primes
python-3.x primes
asked Nov 11 at 6:11
LamerLink
33
33
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
1
down vote
accepted
As he said, you werent correctly checking for prime numbers.
from random import randrange
from time import sleep
list_prime =
user_num = 0
def prime():
user_num = eval(input("Input a number: "))
for i in range(2,user_num):
j = 2
isprime = 1
while (j <= i/2):
if (i % j == 0):
isprime = 0
break
j+=1
if (isprime == 1):
list_prime.append(i)
prime()
if list_prime == '':
print('No prime numbers.')
if list_prime != '':
print('nPrime numbers from 1 to ' + str(user_num) + ': ' + str(list_prime))
sleep(1)
print('nClosing console in 60 seconds...')
sleep(60)
Thanks that works perfectly! Took me a minute to comprehend the syntax but it's very clever. Thanks again.
– LamerLink
Nov 11 at 14:12
add a comment |
up vote
0
down vote
Checking if the result of a modulo operation is 1 is not the correct approach. For example, 6 % 5 is 1, but 6 definitely isn't a prime number. Instead, for each suspect N you should check that no number X exists such that N % X == 0.
A wildly sub-optimal implementation could look like this:
list_prime =
for i in range(2, user_num):
if all(n % x for x in list_prime):
list_prime.append(i)
Thanks. What makes this sub-optimal?
– LamerLink
Nov 11 at 13:42
Algorithmically, it performs as O(n^2) and heuristically, it involves a lot of unnecessary work. For example, if you've already tested if a number is divisible by 2 and 3 then testing for divisibility by 4, 6, 9, and any other multiples of 2 and 3 is pointless.
– billmill
Nov 25 at 7:54
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
As he said, you werent correctly checking for prime numbers.
from random import randrange
from time import sleep
list_prime =
user_num = 0
def prime():
user_num = eval(input("Input a number: "))
for i in range(2,user_num):
j = 2
isprime = 1
while (j <= i/2):
if (i % j == 0):
isprime = 0
break
j+=1
if (isprime == 1):
list_prime.append(i)
prime()
if list_prime == '':
print('No prime numbers.')
if list_prime != '':
print('nPrime numbers from 1 to ' + str(user_num) + ': ' + str(list_prime))
sleep(1)
print('nClosing console in 60 seconds...')
sleep(60)
Thanks that works perfectly! Took me a minute to comprehend the syntax but it's very clever. Thanks again.
– LamerLink
Nov 11 at 14:12
add a comment |
up vote
1
down vote
accepted
As he said, you werent correctly checking for prime numbers.
from random import randrange
from time import sleep
list_prime =
user_num = 0
def prime():
user_num = eval(input("Input a number: "))
for i in range(2,user_num):
j = 2
isprime = 1
while (j <= i/2):
if (i % j == 0):
isprime = 0
break
j+=1
if (isprime == 1):
list_prime.append(i)
prime()
if list_prime == '':
print('No prime numbers.')
if list_prime != '':
print('nPrime numbers from 1 to ' + str(user_num) + ': ' + str(list_prime))
sleep(1)
print('nClosing console in 60 seconds...')
sleep(60)
Thanks that works perfectly! Took me a minute to comprehend the syntax but it's very clever. Thanks again.
– LamerLink
Nov 11 at 14:12
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
As he said, you werent correctly checking for prime numbers.
from random import randrange
from time import sleep
list_prime =
user_num = 0
def prime():
user_num = eval(input("Input a number: "))
for i in range(2,user_num):
j = 2
isprime = 1
while (j <= i/2):
if (i % j == 0):
isprime = 0
break
j+=1
if (isprime == 1):
list_prime.append(i)
prime()
if list_prime == '':
print('No prime numbers.')
if list_prime != '':
print('nPrime numbers from 1 to ' + str(user_num) + ': ' + str(list_prime))
sleep(1)
print('nClosing console in 60 seconds...')
sleep(60)
As he said, you werent correctly checking for prime numbers.
from random import randrange
from time import sleep
list_prime =
user_num = 0
def prime():
user_num = eval(input("Input a number: "))
for i in range(2,user_num):
j = 2
isprime = 1
while (j <= i/2):
if (i % j == 0):
isprime = 0
break
j+=1
if (isprime == 1):
list_prime.append(i)
prime()
if list_prime == '':
print('No prime numbers.')
if list_prime != '':
print('nPrime numbers from 1 to ' + str(user_num) + ': ' + str(list_prime))
sleep(1)
print('nClosing console in 60 seconds...')
sleep(60)
answered Nov 11 at 6:50
Simranjeet Singh
261
261
Thanks that works perfectly! Took me a minute to comprehend the syntax but it's very clever. Thanks again.
– LamerLink
Nov 11 at 14:12
add a comment |
Thanks that works perfectly! Took me a minute to comprehend the syntax but it's very clever. Thanks again.
– LamerLink
Nov 11 at 14:12
Thanks that works perfectly! Took me a minute to comprehend the syntax but it's very clever. Thanks again.
– LamerLink
Nov 11 at 14:12
Thanks that works perfectly! Took me a minute to comprehend the syntax but it's very clever. Thanks again.
– LamerLink
Nov 11 at 14:12
add a comment |
up vote
0
down vote
Checking if the result of a modulo operation is 1 is not the correct approach. For example, 6 % 5 is 1, but 6 definitely isn't a prime number. Instead, for each suspect N you should check that no number X exists such that N % X == 0.
A wildly sub-optimal implementation could look like this:
list_prime =
for i in range(2, user_num):
if all(n % x for x in list_prime):
list_prime.append(i)
Thanks. What makes this sub-optimal?
– LamerLink
Nov 11 at 13:42
Algorithmically, it performs as O(n^2) and heuristically, it involves a lot of unnecessary work. For example, if you've already tested if a number is divisible by 2 and 3 then testing for divisibility by 4, 6, 9, and any other multiples of 2 and 3 is pointless.
– billmill
Nov 25 at 7:54
add a comment |
up vote
0
down vote
Checking if the result of a modulo operation is 1 is not the correct approach. For example, 6 % 5 is 1, but 6 definitely isn't a prime number. Instead, for each suspect N you should check that no number X exists such that N % X == 0.
A wildly sub-optimal implementation could look like this:
list_prime =
for i in range(2, user_num):
if all(n % x for x in list_prime):
list_prime.append(i)
Thanks. What makes this sub-optimal?
– LamerLink
Nov 11 at 13:42
Algorithmically, it performs as O(n^2) and heuristically, it involves a lot of unnecessary work. For example, if you've already tested if a number is divisible by 2 and 3 then testing for divisibility by 4, 6, 9, and any other multiples of 2 and 3 is pointless.
– billmill
Nov 25 at 7:54
add a comment |
up vote
0
down vote
up vote
0
down vote
Checking if the result of a modulo operation is 1 is not the correct approach. For example, 6 % 5 is 1, but 6 definitely isn't a prime number. Instead, for each suspect N you should check that no number X exists such that N % X == 0.
A wildly sub-optimal implementation could look like this:
list_prime =
for i in range(2, user_num):
if all(n % x for x in list_prime):
list_prime.append(i)
Checking if the result of a modulo operation is 1 is not the correct approach. For example, 6 % 5 is 1, but 6 definitely isn't a prime number. Instead, for each suspect N you should check that no number X exists such that N % X == 0.
A wildly sub-optimal implementation could look like this:
list_prime =
for i in range(2, user_num):
if all(n % x for x in list_prime):
list_prime.append(i)
answered Nov 11 at 6:26
Mureinik
175k21126194
175k21126194
Thanks. What makes this sub-optimal?
– LamerLink
Nov 11 at 13:42
Algorithmically, it performs as O(n^2) and heuristically, it involves a lot of unnecessary work. For example, if you've already tested if a number is divisible by 2 and 3 then testing for divisibility by 4, 6, 9, and any other multiples of 2 and 3 is pointless.
– billmill
Nov 25 at 7:54
add a comment |
Thanks. What makes this sub-optimal?
– LamerLink
Nov 11 at 13:42
Algorithmically, it performs as O(n^2) and heuristically, it involves a lot of unnecessary work. For example, if you've already tested if a number is divisible by 2 and 3 then testing for divisibility by 4, 6, 9, and any other multiples of 2 and 3 is pointless.
– billmill
Nov 25 at 7:54
Thanks. What makes this sub-optimal?
– LamerLink
Nov 11 at 13:42
Thanks. What makes this sub-optimal?
– LamerLink
Nov 11 at 13:42
Algorithmically, it performs as O(n^2) and heuristically, it involves a lot of unnecessary work. For example, if you've already tested if a number is divisible by 2 and 3 then testing for divisibility by 4, 6, 9, and any other multiples of 2 and 3 is pointless.
– billmill
Nov 25 at 7:54
Algorithmically, it performs as O(n^2) and heuristically, it involves a lot of unnecessary work. For example, if you've already tested if a number is divisible by 2 and 3 then testing for divisibility by 4, 6, 9, and any other multiples of 2 and 3 is pointless.
– billmill
Nov 25 at 7:54
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53246320%2ftesting-divisibility-of-a-list-and-appending-if-prime%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown