473,396 Members | 1,775 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 Function

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 and 0 if it isn't. I have spent ages just getting this code to work and i'm worried i need to rip it to bit to get this function to work ! The function is:

int prime(int number)

here is my code so far:

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>    //standard header file
  2. #include <conio.h>    //header file
  3. #define maxp 250    //defines "maxp" as a value of 250 //
  4.  
  5. int main()            //start of main program
  6. {
  7.   int factor;        //declares "factor" as an integer
  8.   int primefound;    //declares "primefound" as an integer
  9.   int i;            //declares "i" as an integer
  10.   int l;            //declares "l" as an integer
  11.   int number;        //declares "number" as an integer (the number to be tested)
  12.  
  13.   int primearray[maxp];//declares an array of size determined by "maxp" (250)
  14.   int c;        //declares c as an integer (which is used has a counter)
  15.   c=0;                //initialises c = 0
  16.   number=1;        //initialises the number = 1
  17.  
  18. while(c<maxp)    // while is set up so that the loop executes whilst 
  19.         / c is lesss than 250(i.e. until 250 have been found)                            
  20.   { 
  21.     number++;        //increments the number under test
  22.     primefound=1;        //initialises primefound = 1
  23.     for(i=2;i<number;i++)    //this for loop increments i whilst checking to see if prime
  24.     {
  25.     factor = number%i;    //by checking the remainder determines if prime or not
  26.     if(factor ==0)        //if no remainder ever occurs then the number is not prime, so: 
  27.           {primefound=0;}    //set primefound to 0
  28.     }
  29.  
  30.     if (primefound!=0)    //if there is a prime number found then
  31.     {
  32.        primearray[c]=number; //put the number into the array in the order found.
  33.        ++c;    // increment c ready for the next prime number to be found
  34.     }
  35.   }
  36.   for(l=0;l<maxp;l++)    //this loop is used so that the whole array can be printed
  37.   {
  38.  
  39.    printf("%d\n",primearray[l]);//prints the values in the array in order to the screen
  40.  
  41.   }
  42.  
  43.   getch();
  44. }

help please !

the second function is
void displayprime(int prime[])

which has to display the prime numbers in columns on the screen

can someone point me in the right direction ?
Dec 4 '06 #1
4 5198
willakawill
1,646 1GB
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 and 0 if it isn't. I have spent ages just getting this code to work and i'm worried i need to rip it to bit to get this function to work ! The function is:

int prime(int number)

here is my code so far:

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>    //standard header file
  2. #include <conio.h>    //header file
  3. #define maxp 250    //defines "maxp" as a value of 250 //
  4.  
  5. int main()            //start of main program
  6. {
  7.   int factor;        //declares "factor" as an integer
  8.   int primefound;    //declares "primefound" as an integer
  9.   int i;            //declares "i" as an integer
  10.   int l;            //declares "l" as an integer
  11.   int number;        //declares "number" as an integer (the number to be tested)
  12.  
  13.   int primearray[maxp];//declares an array of size determined by "maxp" (250)
  14.   int c;        //declares c as an integer (which is used has a counter)
  15.   c=0;                //initialises c = 0
  16.   number=1;        //initialises the number = 1
  17.  
  18. while(c<maxp)    // while is set up so that the loop executes whilst 
  19.         / c is lesss than 250(i.e. until 250 have been found)                            
  20.   { 
  21.     number++;        //increments the number under test
  22.     primefound=1;        //initialises primefound = 1
  23.     for(i=2;i<number;i++)    //this for loop increments i whilst checking to see if prime
  24.     {
  25.     factor = number%i;    //by checking the remainder determines if prime or not
  26.     if(factor ==0)        //if no remainder ever occurs then the number is not prime, so: 
  27.           {primefound=0;}    //set primefound to 0
  28.     }
  29.  
  30.     if (primefound!=0)    //if there is a prime number found then
  31.     {
  32.        primearray[c]=number; //put the number into the array in the order found.
  33.        ++c;    // increment c ready for the next prime number to be found
  34.     }
  35.   }
  36.   for(l=0;l<maxp;l++)    //this loop is used so that the whole array can be printed
  37.   {
  38.  
  39.    printf("%d\n",primearray[l]);//prints the values in the array in order to the screen
  40.  
  41.   }
  42.  
  43.   getch();
  44. }

help please !

the second function is
void displayprime(int prime[])

which has to display the prime numbers in columns on the screen

can someone point me in the right direction ?
Hi,
Expand|Select|Wrap|Line Numbers
  1. if(factor ==0)
  2.  {primefound=0;}
in plain english; if there is no factor then this is not a prime number
should be;
Expand|Select|Wrap|Line Numbers
  1. if(factor ==0)        
  2. {primefound=1;}
in plain english; if there is no factor then this is a prime number
Dec 4 '06 #2
Hi,
Expand|Select|Wrap|Line Numbers
  1. if(factor ==0)
  2.  {primefound=0;}
in plain english; if there is no factor then this is not a prime number
should be;
Expand|Select|Wrap|Line Numbers
  1. if(factor ==0)        
  2. {primefound=1;}
in plain english; if there is no factor then this is a prime number

this code works fine tho, i just need to implement the functions
Dec 5 '06 #3
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 and 0 if it isn't. I have spent ages just getting this code to work and i'm worried i need to rip it to bit to get this function to work ! The function is:

int prime(int number)

here is my code so far:

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>    //standard header file
  2. #include <conio.h>    //header file
  3. #define maxp 250    //defines "maxp" as a value of 250 //
  4.  
  5. int main()            //start of main program
  6.  
  7.  
  8.  
  9.  
  10. {
  11.   int factor;        //declares "factor" as an integer
  12.   int primefound;    //declares "primefound" as an integer
  13.   int i;            //declares "i" as an integer
  14.   int l;            //declares "l" as an integer
  15.   int number;        //declares "number" as an integer (the number to be tested)
  16.  
  17.   int primearray[maxp];//declares an array of size determined by "maxp" (250)
  18.   int c;        //declares c as an integer (which is used has a counter)
  19.   c=0;                //initialises c = 0
  20.   number=1;        //initialises the number = 1
  21.  
  22. while(c<maxp)    // while is set up so that the loop executes whilst 
  23.         / c is lesss than 250(i.e. until 250 have been found)                            
  24.   { 
  25.     number++;        //increments the number under test
  26.     primefound=1;        //initialises primefound = 1
  27.     for(i=2;i<number;i++)    //this for loop increments i whilst checking to see if prime
  28.     {
  29.     factor = number%i;    //by checking the remainder determines if prime or not
  30.     if(factor ==0)        //if no remainder ever occurs then the number is not prime, so: 
  31.           {primefound=0;}    //set primefound to 0
  32.     }
  33.  
  34.     if (primefound!=0)    //if there is a prime number found then
  35.     {
  36.        primearray[c]=number; //put the number into the array in the order found.
  37.        ++c;    // increment c ready for the next prime number to be found
  38.     }
  39.   }
  40.   for(l=0;l<maxp;l++)    //this loop is used so that the whole array can be printed
  41.   {
  42.  
  43.    printf("%d\n",primearray[l]);//prints the values in the array in order to the screen
  44.  
  45.   }
  46.  
  47.   getch();
  48. }

help please !

the second function is
void displayprime(int prime[])

which has to display the prime numbers in columns on the screen

can someone point me in the right direction ?
Hi ,

You can declare the function as You have done

int Prime (int TestNumber);
implementation:

Expand|Select|Wrap|Line Numbers
  1. int Prime (int TestNumber)
  2. {
  3.  
  4.   int IsPrime; 
  5.   // Here write the logic for testing a number to be prime and return 
  6.  // set value of IsPrime accordingly in logic
  7.  return IsPrime;
  8. }
  9.  
Second fuction is
Expand|Select|Wrap|Line Numbers
  1.  
  2. void displayprime(int prime[], int nTotalPrimes)
  3. {
  4.   for (int i = 0; i< nTotalPrimes; i++
  5.   prinf ("%d", prime[i]);
  6. }
  7.  
  8.  
Dec 5 '06 #4
DeMan
1,806 1GB
Are you having problems with the writing method part or the logic part?

The logic for the method would be: given an integer x test whether it is divisible by any number below it (or if we're really keen, any number below the square root of it, in fact any prime number, but now we digress and get complicated).

In rough pseudocode this would be:
Expand|Select|Wrap|Line Numbers
  1. int isPrime(int x)
  2. {
  3.   int counter=2;
  4.   int found=0;
  5.   while((counter*counter)<=x &&(found==0))
  6.   {
  7.     if((x mod counter)==0) found =1; //Or if you don't mind 
  8.     counter++
  9.   }
  10. return found;
  11. }
  12.  
How many columns did you want to display the primes in ?
Simplest way is tyo iterate through the array displaying 1 at a time adding "/n" to your print statement each time you need to start a new row and using "/t" to line numbers up on the screen....

And sorry, I was beaten to it, didn't realise,.....
Dec 5 '06 #5

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,...
5
by: Rahul | last post by:
HI. Python , with its support of arbit precision integers, can be great for number theory. So i tried writing a program for testing whether a number is prime or not. But then found my function...
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...
0
by: AshifToday | last post by:
this was my and my frineds little project in earlier classes, the program seperates the composite and prime numbers in two sections of the screen ===================== /* This program has...
7
by: brian.digipimp | last post by:
Write a program that prompts the user to input a positive integer. It should then output a message indicating whether the number is a prime number. (Note: An even number is prime if it is 2. An odd...
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...
12
by: electric916 | last post by:
I have a homework assignment i Am totally confused on. I started with a basic code to determine if a number is prime or not, but need guidance from here. I will post assignment details then what I...
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
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
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
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.