I've been having problem with the following code. It's supposed to
print the prime numbers between 10 and 100. But i'm not getting any
output, i.e. i guess the outer 'for' loop is being traversed only
once. I would be greatful if you could help me out. Thanx! -
>>> f=1
-
>>> for i in range(10,100):
-
-
... for j in range(2,i):
-
... if i%j==0:
-
... f=0
-
... break
-
... else: continue
-
... if f==1:
-
... print i,
-
...
3 1588
I've been having problem with the following code. It's supposed to
print the prime numbers between 10 and 100. But i'm not getting any
output, i.e. i guess the outer 'for' loop is being traversed only
once. I would be greatful if you could help me out. Thanx! -
>>> f=1
-
>>> for i in range(10,100):
-
-
... for j in range(2,i):
-
... if i%j==0:
-
... f=0
-
... break
-
... else: continue
-
... if f==1:
-
... print i,
-
...
I played with your code a bit, but didn't get anywhere.
See this discussion on generating a list of prime numbers.
I've been having problem with the following code. It's supposed to
print the prime numbers between 10 and 100. But i'm not getting any
output, i.e. i guess the outer 'for' loop is being traversed only
once. I would be greatful if you could help me out. Thanx! -
>>> f=1
-
>>> for i in range(10,100):
-
-
... for j in range(2,i):
-
... if i%j==0:
-
... f=0
-
... break
-
... else: continue
-
... if f==1:
-
... print i,
-
...
I think your problem is you never set 'f' back to one again so it will always be zero after the 10, which isn't prime. Try this: -
for i in range(10, 100):
-
for j in range(2, i):
-
if not i % j:
-
break
-
-
else:
-
print i
-
As you see, it doesn't use an unnecessary variable.
I like the Sieve of Eratosthenes. Not the fastest algorithm around, But it can be made wery clean in python: -
>>> primes = range(10,100)
-
>>> for i in range(2, 8):
-
... primes = filter(lambda x: x == i or x % i, primes)
-
...
-
>>> print primes
-
[11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]
-
>>>
-
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Greg Brunet |
last post by:
In doing some testing of different but simple algorithms for getting a
list of prime numbers, I ended up getting some results that seem a bit
contradictory. Given the following test program...
|
by: Tuvas |
last post by:
I have made and recently posted a libary I made to do Modular
Arithmetic and Prime numbers on my website at
http://www.geocities.com/brp13/Python/index.html . I am currently in a
crypotology...
|
by: uncensored |
last post by:
Hello everyone,
I'm fairly new at .Net and I have a repeater inside a repeater problem. I will attach my code to this message but basically what I am able to tell when I run my page it tells me...
|
by: flash1600 |
last post by:
I am using Access 97, yes I know Access 97 not by choice, I am adding a
new record to a table with a Primary Key set with an auto number
incremented by 1. I have the following code to find the...
|
by: SweetLeftFoot |
last post by:
Hello, i have designed some code that works out the first 250 prime numbers and prints them to the screen. However i need to implement 2 functions, one of which returns a 1 if the number is a prime...
|
by: Joel Mayes |
last post by:
Hi All;
I'm teaching myself C, and have written a prime number generator. It is
a pretty inefficient implementation of the Sieve of Eratosthenes to
calculate primes up to 1,000,000. If anyone...
|
by: maks |
last post by:
Hi!
I need some help in modifying this prime number generator code. How do I modify this code so that it assigns prime numbers to an array and returns it?
I have tried to get it work but it...
|
by: bartonc |
last post by:
Description:
This is a fast prime number list generator using sieve algorithm. This function return a list of prime numbers which <= argument.
def primes(n):
if n==2: return
elif n<2:...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |