473,396 Members | 2,030 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,396 software developers and data experts.

Prime Number!! Emergency

21
I need help from you all to solve my assignment. I want to display only prime number start from 1 to 100.I hope you guys solve this post fast, its emergency.
Thanks for your help
Nov 13 '06 #1
7 9015
r035198x
13,262 8TB
I need help from you all to solve my assignment. I want to display only prime number start from 1 to 100.I hope you guys solve this post fast, its emergency.
Thanks for your help
If you are not willing to get help on doing it yourself, then search this forum and the java forum. It has been done many times in those locations
Nov 13 '06 #2
I need help from you all to solve my assignment. I want to display only prime number start from 1 to 100.I hope you guys solve this post fast, its emergency.
Thanks for your help
i will help u out

#include<stdio.h>
#include<conio.h>
main()
{
int i,p=0,j;
for(i=1;i<=100;i++)
for(j=1;j<=i;j++)
{
if(i%j==0)
p++;
}
if(p==2)
printf("%d",i);
p=0;
}
getch();
}
Nov 13 '06 #3
i will help u out
for(i=1;i<=100;i++)
for(j=1;j<=i;j++)
{
if(i%j==0)
p++;
}
if(p==2)
printf("%d",i);
p=0;
}
this code definitely works. a prime number is one which only has two divisors - 1 and itself. when p==2 which is the minimum that p will equal, then i is a prime number.

to speed up the code though you may want your second "for" statement to be
Expand|Select|Wrap|Line Numbers
  1.  
  2. for(j=2;j<i;j++)...
  3.  
  4. if(p==0)...
  5.  
  6.  
this only gets rid of a couple of rounds. if you really want to start saving some time, you need to set a dynamic limit to how high j can go. this is because you will find all divisors of a number by the time that you reach the square root. for example. numbers that divide 100 are:

1,2,4,5,10,20,25,50,100

or in their pairs they are:

1*100
2*50
4*25
5*20
10*10

notice that by the time that you divide 100 by 10 you have found every divisor.
this means that instead of 100 divides to find a prime you only need 10 or 9 because we will start at 2.

you can actually save even more time, but its really not worth it unless you are looking for a lot of prime numbers. i have only one other post on here and it is in answer to this same question. i implemented a vector to keep track of the prime numbers as it found them and then only used those numbers to divide and find other primes - after all why divide a number by 6 when you have already divided it by 2 and 3. if it is not divisable by 2 or 3 then it is not by 6. implementing this method would reduce the amount of divides for the number 97(which is prime) down to about 4. it only need be divided by 2,3,5,7. the numbers 8,9,10 are all themselves divisable by these 4 numbers and therefore don't need to be used, and no need to go above 10 because it is about the square root. you can look at the code at prime number code

i hope this makes sense
Nov 14 '06 #4
Budiman
21
Thx for all your help.
Nov 18 '06 #5
In this problem, there's no need to check whether the number is divisible by all number upto the given number. that is to check whether n is prime number or not, its enough to check whether it is divisible by a number between 2 to n/2. This will speed up the program a lot;

void main()
{
int i,j,n,b=1;
printf("\n Enter the range:");
scanf("%d",&n);
for(i=2;i<=n;i++)
{
for(j=2;j<i/2;j++)
{
if(i%j==0)
{
b+=1;
}
}
if(b)
printf("\n %d",i);
}
getch();
}
Nov 18 '06 #6
macklin01
145 100+
In this problem, there's no need to check whether the number is divisible by all number upto the given number. that is to check whether n is prime number or not, its enough to check whether it is divisible by a number between 2 to n/2. This will speed up the program a lot;
Actually, it's sufficient to only check known primes between 2 and sqrt(n). If you're generating a list of all prime numbers between 2 and N, starting at 2, then to check any given number n <= N, you only have to check for prime divisors up to an including sqrt(n), which you will have recorded in a list of known prime numbers.

Better still, just use the Sieve of Erastothenes algorithm. (Google). It's pretty much the fastest technique to determine all the primes in a range [2,N].

It's too bad the original poster wasn't willing to do some of the work on this, and that others chose to do it for him. Prime number testing is a fun and rich topic, and always relevant to things like encryption. Many programmers cut their teeth on writing prime number-related programs. Very good logic exercises, and lots of fun. -- Paul
Nov 18 '06 #7
#include <stdio.h>


int main(void){
int j,Lim,i,flag;
printf("Please enter a limit number : ");
scanf("%d",&Lim);

for(i=2;i<=Lim;i++){
for(j=2;j<=i-1;j++){
if(i%j==0){
flag=0;
break;}
flag=1;
}
if(flag==1)
printf("%5d\t",i, "%5d",i);
}

return 0;
}
Nov 18 '06 #8

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

Similar topics

36
by: Dag | last post by:
Is there a python module that includes functions for working with prime numbers? I mainly need A function that returns the Nth prime number and that returns how many prime numbers are less than N,...
11
by: lostinpython | last post by:
I'm having trouble writing a program that figures out a prime number. Does anyone have an idea on how to write it? All I know is that n > 2 is prim if no number between 2 and sqrt of n...
11
by: don | last post by:
Ok, this is a homework assignment, but can you help me out anyway...... I need a routine for figuring out if a number inputted by the user is a prime number or not...... all I'm asking for is Not...
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...
7
by: Caffiend | last post by:
Well, I've been picking at learning python, got tired of reading, and figured I'd try to replicate my prime number generator I wrote (with much TSDN forum help) in C++. I've hit a stumbling block......
2
by: QHorizon | last post by:
Hello, I'm new to Python (I've learned everything up to iterators so far) and fairly new to Programming. This would be my first real program: #Coordinate Geometry (The whole program is not...
5
by: silversnake | last post by:
I'm trying to write a program that take a input number and prints if is a prime numbers but is not working for instance, it says that 4 is prime while 5 is not. can anyone see what the problem is ....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: 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
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
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.