473,399 Members | 3,832 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,399 software developers and data experts.

how to print first n primes?

hey there!!!

we were asked to make a code for printing prime numbers from 3 until the [i]n[i]th prime number but i can't seem to find the right solution for this one.

when i print my work it prints some other numbers that has the factor of another prime number

it prints the numbers:
3, 5, 7, 9 ,11, 13, 15...

i'm a beginner here, so please help me.
i really would appreciate your helping me.
thanks.
May 12 '10 #1
6 6660
Dheeraj Joshi
1,123 Expert 1GB
Does it print
Expand|Select|Wrap|Line Numbers
  1. 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59
  2.  
this?

These are prime numbers.
Can we have a look at your code?

Regards
Dheeraj Joshi
May 12 '10 #2
Banfa
9,065 Expert Mod 8TB
Well from the look of your output you are just printing all the odd numbers rather than checking if a number is prime before printing it.
May 12 '10 #3
donbock
2,426 Expert 2GB
The basic logic of your program is
Expand|Select|Wrap|Line Numbers
  1.    number_of_primes = 0;
  2.    candidate = 1;
  3.    Repeat until number_of_primes == threshold {
  4.       if candidate is a prime {
  5.          print the candidate
  6.          increment number of primes
  7.       }
  8.    }
The challenge is determining if the candidate is a prime. Forget about computer programs for a moment. Let's talk mathematics first: how can you tell if a number is prime? There is no point writing code until you have a firm grasp on this.
May 12 '10 #4
actually...i've already got how to print prime numbers but the next problem i'm having now is how to print the first n primes, not the prime numbers from 0 to n.

i really did appreaciate your replies.

this is my code:
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h> 
  2.  
  3. main() 
  4. int x,y,n; 
  5. printf(" Enter n: "); 
  6. scanf("%d",&n); 
  7. printf("\n"); 
  8.  
  9.  
  10. for(x=3;x<=n;x++) 
  11. {
  12. for(y=2;y<=x-1;y++) 
  13. if(x%y==0) 
  14. break; 
  15. if(x==y) 
  16. printf("\t%d\n",x);
  17. }
  18.  
  19.  
  20. }

i really would like your help...thanks
May 13 '10 #5
Banfa
9,065 Expert Mod 8TB
In your for loop instead of the end condition being a test against x, the current number you are checking, you need to use a separate variable to keep a count of the number of primes you have found and test against that instead.

Also your loop at line 13 works but has a lot of scope for optimisation.
May 13 '10 #6
donbock
2,426 Expert 2GB
Both loops involve comparisons against x. The one Banfa wants you to change is the outermost loop at line 11.

By the way, "2' is a prime number too.
May 13 '10 #7

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

Similar topics

20
by: MickeyBob | last post by:
How does one detect the EOF gracefully? Assuming I have a pickle file containing an unknown number of objects, how can I read (i.e., pickle.load()) until the EOF is encountered without generating...
1
by: avinashc | last post by:
If anyone is interested in a /etc/hosts.deny automatic update script (Unix only) based on sshd/vsftpd attacks, here's a python script: http://www.aczoom.com/tools/blockhosts/ This is a beta...
1
by: mayFlower | last post by:
Overloaded ostream << operator doesnt print first stream C++ .Net Framework. For example : ostream os; os <<"Hello"<<"World"<<ends; It will print something like: 0x127aa123World. Is this...
1
by: Hemanth | last post by:
Hello, How do I find the first and last non-numeric char positions (using regexp) in an alphanumeric string? For example, 99ABC1A => should return 2, 6 DE8A1 => should return 0, 3 Thanks,
2
by: Karlo Lozovina | last post by:
Consider this short script: --- from time import time, sleep st = time() print 'Start: %f, ' % st, sleep(10) sp = time() print 'Stop: %f, Duration: %f' % (sp, (st - sp))
2
by: lekshminair | last post by:
hello friends, can u help me. how to print first string in a line.(before first space) for example: String str={"Hello world java"} output Hello
3
by: AWasilenko | last post by:
I'm still in the process of learning python via a handful of books I bought. One book I am reading just introduced Base Class Methods. I found that I needed more understanding on this concept and...
1
by: nivaz | last post by:
hai friends, i really want to know how to read a file seperately. that means i want to read and print first 24 lines then if i click the next button i will read and print tihe next 24 lines. try...
5
by: sniipe | last post by:
Hi, I have a problem with unicode string in Pylons templates(Mako). I will print first char from my string encoded in UTF-8 and urllib.quote(), for example string 'Łukasz': ...
0
by: Tim Chase | last post by:
I need to put in the var property of the first object from the list assuming you meant "foo = any.name if ***any*** is not None else beny.name..." If you have a fixed/hard-coded list of...
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: 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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.