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
7 9015
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
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();
}
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 -
-
for(j=2;j<i;j++)...
-
-
if(p==0)...
-
-
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
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();
}
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
#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;
}
Sign in to post your reply or Sign up for a free account.
Similar topics
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,...
|
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...
|
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...
|
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: 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......
|
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...
|
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 ....
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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: 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: 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: 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: 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...
| |