473,395 Members | 1,783 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,395 software developers and data experts.

prime number generator

can any one give me the code for generating prime numbers using c program
Jan 21 '07 #1
24 13942
Ganon11
3,652 Expert 2GB
http://www.thescripts.com/forum/thread552744.html

What ideas do you have? What code have you written so far? Please show us what effort you have made to solve the problem on your own, and we will be glad to help you reach the solution.
Jan 21 '07 #2
hey
Even i am trying the same programme.
so heres the code

#include<stdio.h>
main()
{
int a,i;
for(a=3; a<=100; a++)
{
for(i=2; i<a/2; i++)
{
if(a%i==0)
{
break;
}
else
continue;
}
printf("%d\t",a);
}
}


and the output i get are all the integers from 3 to 100.
so tell me where am i going wrong.
Feb 28 '07 #3
DeMan
1,806 1GB
Someone suggested in another thread, that you may like to create a method isPrime (or something similar) that tests whether a number is prime. [Remember a number is prime if it is not divisible by any other prime -> and we know that the largest number that could possibly divide a number is sqrt of the number]

then you loop once through the hundred and hve something like
Expand|Select|Wrap|Line Numbers
  1. if(isPrime(n))
  2. {
  3.   printf(n);
  4. }
  5.  
in your loop. Hopefully the thread Ganon pointed you toward has moreinformation about this.....
Feb 28 '07 #4
Ganon11
3,652 Expert 2GB
Hopefully the thread Ganon pointed you toward has moreinformation about this.....
Actually, it was the thread for posting guidelines - I should probably include this link to the FAQ, as it is more recent/appropriate.
Feb 28 '07 #5
scruggsy
147 100+
Think about what makes a number prime: It is not evenly divisible by any other prime number.
Conveniently, your program is supposed to find prime numbers.
So if you store each prime number found in an array, you can use that list to compare against. When you find a number that's not divisible by any of the prime numbers on your list, add it to the array.
Mar 1 '07 #6
DeMan
1,806 1GB
Actually, it was the thread for posting guidelines - I should probably include this link to the FAQ, as it is more recent/appropriate.
My bad!! I knew it had been discussed before and ASSUMED without checking - i guess we lives and learns.....
Mar 1 '07 #7
Savage
1,764 Expert 1GB
My bad!! I knew it had been discussed before and ASSUMED without checking - i guess we lives and learns.....
Maybe I have solution:

Any number that can't be divisible with 2 and 3 is a prime number.

SO if you could make if expession like if(a%2!=0&&a%3!=0) and then put it in
array you should get a prime number generator.
Mar 1 '07 #8
to improve the efficiency, you modify the inner for loop by

for(i=2;i<sqrt(a);i++)
{
......
......
.....
}
Mar 1 '07 #9
Ganon11
3,652 Expert 2GB
Maybe I have solution:

Any number that can't be divisible with 2 and 3 is a prime number.
Not true. Consider the number 35. It's not divisible by 2 (35 / 2 = 17.5) or 3 (35 / 3 = 11.667), but it is divisible by 7 (35 / 7 = 5) and 5 (35 / 5 = 7). Thus, 35 is not prime - but your test would say it is.
Mar 1 '07 #10
ChaseCox
294 100+
A prime number is defined as a number that is only divisible evenly by itself and one. Meaning numbers like 1,2,3,5,7...41,83...ect. To save on efficiency, you can eliminate all evens, except two, and any odd number that is evenly divible by 3,5,7,9,11...ect. The resulting numbers should all be prime.
Mar 1 '07 #11
Savage
1,764 Expert 1GB
Not true. Consider the number 35. It's not divisible by 2 (35 / 2 = 17.5) or 3 (35 / 3 = 11.667), but it is divisible by 7 (35 / 7 = 5) and 5 (35 / 5 = 7). Thus, 35 is not prime - but your test would say it is.
That's true.But what if we add as a divider number 5 when our "a" increases beyond value of 5.Then,the code should look like:


if(a>5) if(a%2!=0&&a%3!=0&&a%5!=0) put it in array
else if(a==3) put it in array.
Mar 1 '07 #12
ChaseCox
294 100+
That's true.But what if we add as a divider number 5 when our "a" increases beyond value of 5.Then,the code should look like:


if(a>5) if(a%2!=0&&a%3!=0&&a%5!=0) put it in array
else if(a==3) put it in array.
Right, but 5 is prime, as is 7. So my thoughts would be to create an array of only odd numbers. Then divide each individual part of the array, by the rest of the array. If the only number it is divisble by is in the same array location as the one you are currently opperating in, then that is a prime number. Do not try and use all of those if statements, it will be computationaly poor. Always try and utilize the arrays.
Mar 1 '07 #13
sicarie
4,677 Expert Mod 4TB
Right, but 5 is prime, as is 7. So my thoughts would be to create an array of only odd numbers. Then divide each individual part of the array, by the rest of the array. If the only number it is divisble by is in the same array location as the one you are currently opperating in, then that is a prime number. Do not try and use all of those if statements, it will be computationaly poor. Always try and utilize the arrays.
That's a lot of work to set up, why not just use a for loop that counts up to the number, and if it divides, then drop it, if it doesn't, put it into your array.
Mar 1 '07 #14
ChaseCox
294 100+
That's a lot of work to set up, why not just use a for loop that counts up to the number, and if it divides, then drop it, if it doesn't, put it into your array.
Design one loop to initialize your array of odd numbers
this should equal 3 lines of code

Design another loop that divides your current element, by the entire array. (You need to be sure it only returns evenly divised integers.)
this should also equal 3-4 lines of code


If any integer value in your new array is greater than 1, then your number is not prime
Mar 1 '07 #15
Ganon11
3,652 Expert 2GB
How about this pseudocode:

Expand|Select|Wrap|Line Numbers
  1. Loop X from START to END (We are testing X to see if it is prime):
  2.    (Right now, X is prime as far as we know.)
  3.    bool isPrime = true;
  4.    Loop i from 2 to X (Don't include 1 or 0, because dividing by 0 gives error, and dividing by 1 always has no remainder)
  5.       If X is evenly divisible by i
  6.          X is not prime.
  7.          Change isPrime to false, break from loop (no need to continue).
  8.       Else
  9.          Take no action (Can't be sure it will remain prime.)
  10.    End Loop
  11.    (Now, isPrime correctly indicates X's state).
  12.    If X is prime
  13.       Take appropriate action
  14.    Else
  15.       Take appropriate action
  16. End Loop
Mar 1 '07 #16
Savage
1,764 Expert 1GB
How about this pseudocode:

Expand|Select|Wrap|Line Numbers
  1. Loop X from START to END (We are testing X to see if it is prime):
  2.    (Right now, X is prime as far as we know.)
  3.    bool isPrime = true;
  4.    Loop i from 2 to X (Don't include 1 or 0, because dividing by 0 gives error, and dividing by 1 always has no remainder)
  5.       If X is evenly divisible by i
  6.          X is not prime.
  7.          Change isPrime to false, break from loop (no need to continue).
  8.       Else
  9.          Take no action (Can't be sure it will remain prime.)
  10.    End Loop
  11.    (Now, isPrime correctly indicates X's state).
  12.    If X is prime
  13.       Take appropriate action
  14.    Else
  15.       Take appropriate action
  16. End Loop
Yes,that is nearly giving the corect output,if we by first else statement insert the
next code:

Expand|Select|Wrap|Line Numbers
  1.                 else{   if(x%3==0||x%5==0||x%7==0) break;
  2.                     printf("\t%d",x);
  3.                     delay(50);
  4.                     isPrime=TRUE;
  5.                     break;
  6.                                 }
,and we before first loop:

Expand|Select|Wrap|Line Numbers
  1. printf("3\t5\t7");
the output in range of 3-100 is corect.


Savage.
Mar 1 '07 #17
You are all wrong. The fact that a number can't be divided by two or three does not mean it is prime. 49 can't be divided by 2 nor 3 but it is not prime. If it can be divided by 2 then it is not prime. If it has square root it's not prime. The biggest number a number can divided by is not the square root.
Mar 3 '07 #18
DeMan
1,806 1GB
You are all wrong. The fact that a number can't be divided by two or three does not mean it is prime. 49 can't be divided by 2 nor 3 but it is not prime. If it can be divided by 2 then it is not prime. If it has square root it's not prime. The biggest number a number can divided by is not the square root.
And you have obviously read only part of the thread...

A prime number is defined as a number that is only divisible evenly by itself and one. Meaning numbers like 1,2,3,5,7...41,83...ect. To save on efficiency, you can eliminate all evens, except two, and any odd number that is evenly divible by 3,5,7,9,11...ect. The resulting numbers should all be prime
and again
A prime number is defined as a number that is only divisible evenly by itself and one. Meaning numbers like 1,2,3,5,7...41,83...ect. To save on efficiency, you can eliminate all evens, except two, and any odd number that is evenly divible by 3,5,7,9,11...ect. The resulting numbers should all be prime
also
Not true. Consider the number 35. It's not divisible by 2 (35 / 2 = 17.5) or 3 (35 / 3 = 11.667), but it is divisible by 7 (35 / 7 = 5) and 5 (35 / 5 = 7). Thus, 35 is not prime - but your test would say it is.
Only one or two people actually appeared to say what you claim about primes being any number not divisible by 2 or 3, and they were quickly corrected (see above).



While it is true that the biggest number that divides a number is NOT the smaller than the square root, if a number does have any divisors, AT LEAST one of them MUST be less than the square root, so we only have to test to the square root.
(think about 48, - 8 is larger than sqrt(48) (or approx 6.928). However, 8 * 6 = 48 and 6 is SMALLER than 6.928. Remember we are not looking for ALL factors, we are looking for ANY.....We know that there will always be the same number of factors less than the square root as greater than......so as long as we can show there are none less than, we know there couldn't be any greater than either....)
Mar 3 '07 #19
I've thinking about this and i finally made a program able to list all the combinations possible to for a number by multiplications. It works perfectly.
I am not going to post it now because i don't have the code here. If you want to ask me for it, send an email to me (<email removed>). I will send you a copy of it. Don't wait to see it here because i always forget addresses and might never enter this site again.
Mar 3 '07 #20
And you have obviously read only part of the thread...


and again


also


Only one or two people actually appeared to say what you claim about primes being any number not divisible by 2 or 3, and they were quickly corrected (see above).



While it is true that the biggest number that divides a number is NOT the smaller than the square root, if a number does have any divisors, AT LEAST one of them MUST be less than the square root, so we only have to test to the square root.
(think about 48, - 8 is larger than sqrt(48) (or approx 6.928). However, 8 * 6 = 48 and 6 is SMALLER than 6.928. Remember we are not looking for ALL factors, we are looking for ANY.....We know that there will always be the same number of factors less than the square root as greater than......so as long as we can show there are none less than, we know there couldn't be any greater than either....)
I did know all that. That is what i took into consideration to develop my program. That is the way it works
Mar 3 '07 #21
chella
51
Not true. Consider the number 35. It's not divisible by 2 (35 / 2 = 17.5) or 3 (35 / 3 = 11.667), but it is divisible by 7 (35 / 7 = 5) and 5 (35 / 5 = 7). Thus, 35 is not prime - but your test would say it is.

Hi,
Try this code. It'll generate prime numbers from 2 to 100...

the value 'a' here stores the limit i.e,upto 100 or 200.

Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. int main()
  3. {
  4.    int a,i;
  5.    int flag = 0;
  6.    for(a = 2 ; a <= 100 ;a++){
  7.       for (i = 2; i < a ;i++){
  8.          if (a % i == 0)
  9.             flag = 1;
  10.       }
  11.       if (flag == 0)
  12.          printf("%d\n",a);
  13.       flag =0;
  14.    }
  15.    return 0;
  16. }
Please let me know if there is any issues.

Regards,
chella
Mar 5 '07 #22
DeMan
1,806 1GB
That code sure will.....
Mar 5 '07 #23
ok... i tried that prime number generator and it worked! however....


when i attempted to change it slightly to make it to where anyone could put in any number and recieve back all the prime numbers up to that number, it screwed up on me. here is my changes (im bored and wanted to see if i could make it more useful to someone other than myself)


Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. int main()
  3. {
  4.  
  5.  int target;
  6.  
  7.  printf("What number would you like to see all the prime numbers to?");
  8.  scanf("%d", &target);
  9.  
  10. int a,i;
  11.    int flag = 0;
  12.    for(a = 2 ; a <= 100 ;a++){
  13.       for (i = 2; i < a ;i++){
  14.          if (a % i == 0)
  15.             flag = 1;
  16.       }
  17.       if (flag == 0)
  18.          printf("%d\n",a);
  19.       flag =0;
  20.    }
  21.    return 0;
  22. }
  23.  

and these are the errors i recieved (using Vim in Unix)

primenumbergen.c: In function `main':
primenumbergen.c:12: parse error before `int'
primenumbergen.c:14: `a' undeclared (first use in this function)
primenumbergen.c:14: (Each undeclared identifier is reported only once
primenumbergen.c:14: for each function it appears in.)
primenumbergen.c:15: `i' undeclared (first use in this function)
primenumbergen.c:17: `flag' undeclared (first use in this function)
Mar 6 '07 #24
chella
51
ok... i tried that prime number generator and it worked! however....


when i attempted to change it slightly to make it to where anyone could put in any number and recieve back all the prime numbers up to that number, it screwed up on me. here is my changes (im bored and wanted to see if i could make it more useful to someone other than myself)


Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. int main()
  3. {
  4.  
  5.  int target;
  6.  
  7.  printf("What number would you like to see all the prime numbers to?");
  8.  scanf("%d", &target);
  9.  
  10. int a,i;
  11.    int flag = 0;
  12.    for(a = 2 ; a <= 100 ;a++){
  13.       for (i = 2; i < a ;i++){
  14.          if (a % i == 0)
  15.             flag = 1;
  16.       }
  17.       if (flag == 0)
  18.          printf("%d\n",a);
  19.       flag =0;
  20.    }
  21.    return 0;
  22. }
  23.  

and these are the errors i recieved (using Vim in Unix)

primenumbergen.c: In function `main':
primenumbergen.c:12: parse error before `int'
primenumbergen.c:14: `a' undeclared (first use in this function)
primenumbergen.c:14: (Each undeclared identifier is reported only once
primenumbergen.c:14: for each function it appears in.)
primenumbergen.c:15: `i' undeclared (first use in this function)
primenumbergen.c:17: `flag' undeclared (first use in this function)

Hi,
Declare the int a, i ... before the printf & scanf stmt's.
Hope this will work :-)
Mar 6 '07 #25

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,...
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...
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...
60
by: rhle.freak | last post by:
Here is my code to generate prime numbers.It works absolutely fine when the range is *not very large*. However on initializing i with a large integer it produces erroneous results (some numbers...
7
by: newstips6706 | last post by:
1, 2, 3, 5, 7... PRIME Numbers ________________________________ Definitions What is a PRIME Number ?
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:...
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: sudankanakavel | last post by:
i need a random prime number generator which will generate prime numbers for given range for eg 22222 to 99999 operating system : windows language : java
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
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?
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
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.