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.
6 6660
Does it print -
2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59
-
this?
These are prime numbers.
Can we have a look at your code?
Regards
Dheeraj Joshi
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.
The basic logic of your program is - number_of_primes = 0;
-
candidate = 1;
-
Repeat until number_of_primes == threshold {
-
if candidate is a prime {
-
print the candidate
-
increment number of primes
-
}
-
}
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.
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: - #include<stdio.h>
-
-
main()
-
{
-
int x,y,n;
-
printf(" Enter n: ");
-
scanf("%d",&n);
-
printf("\n");
-
-
-
for(x=3;x<=n;x++)
-
{
-
for(y=2;y<=x-1;y++)
-
if(x%y==0)
-
break;
-
if(x==y)
-
printf("\t%d\n",x);
-
}
-
-
-
}
i really would like your help...thanks
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.
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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,
|
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))
|
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
|
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...
|
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...
|
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':
...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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: 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...
|
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: 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,...
|
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: 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,...
| |