473,386 Members | 1,752 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Problem with following Prime Number Generator code

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!
Expand|Select|Wrap|Line Numbers
  1. >>> f=1
  2. >>> for i in range(10,100):
  3.  
  4. ...     for j in range(2,i):
  5. ...             if i%j==0:
  6. ...                     f=0
  7. ...                     break
  8. ...             else: continue
  9. ...     if f==1:
  10. ...             print i,
  11. ...
Jun 12 '07 #1
3 1588
bartonc
6,596 Expert 4TB
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!
Expand|Select|Wrap|Line Numbers
  1. >>> f=1
  2. >>> for i in range(10,100):
  3.  
  4. ...     for j in range(2,i):
  5. ...             if i%j==0:
  6. ...                     f=0
  7. ...                     break
  8. ...             else: continue
  9. ...     if f==1:
  10. ...             print i,
  11. ...
I played with your code a bit, but didn't get anywhere.
See this discussion on generating a list of prime numbers.
Jun 12 '07 #2
ilikepython
844 Expert 512MB
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!
Expand|Select|Wrap|Line Numbers
  1. >>> f=1
  2. >>> for i in range(10,100):
  3.  
  4. ...     for j in range(2,i):
  5. ...             if i%j==0:
  6. ...                     f=0
  7. ...                     break
  8. ...             else: continue
  9. ...     if f==1:
  10. ...             print i,
  11. ...
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:
Expand|Select|Wrap|Line Numbers
  1. for i in range(10, 100):
  2.     for j in range(2, i):
  3.         if not i % j:
  4.             break
  5.  
  6.     else:
  7.         print i
  8.  
As you see, it doesn't use an unnecessary variable.
Jun 12 '07 #3
Smygis
126 100+
I like the Sieve of Eratosthenes. Not the fastest algorithm around, But it can be made wery clean in python:

Expand|Select|Wrap|Line Numbers
  1. >>> primes = range(10,100)
  2. >>> for i in range(2, 8):
  3. ...     primes = filter(lambda x: x == i or x % i, primes)
  4. ... 
  5. >>> print primes
  6. [11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]
  7. >>> 
  8.  
Jun 12 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

9
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...
20
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...
0
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...
2
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...
4
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...
10
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...
5
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...
4
bartonc
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:...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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...
0
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...
0
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
0
BarryA
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...
1
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...
0
marktang
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,...
0
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...
0
jinu1996
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.