# Prime numbers

• 03-27-2011
cubbies5
Prime numbers
I am terrible with coding, so hopefully one can help.

I am in a basic coding computer science class, so I can only use codes involving do while loops, while loops, and if statements.

The user inputs two bounds and you need to calculate all the prime numbers between these two bounds.
I am having trouble calculating the prime numbers in the bounds.
Currently this is the code i have:
Code:

```#include <stdio.h> void getinput(int*,int*); int numbers(int*, int*); int main() {   int value1;   int value2;   getinput(&value1, &value2);   numbers(&value1, &value2);   return(0); } void getinput(int* value1,int* value2) {   do   {   printf("Enter starting range value:  ");   scanf("%d",value1);   if (*value1 < 0)   {   printf("\nError! Non-negative integers only!\n");   }   }   while(*value1 <  0);   do   {     printf("Enter ending range value: ");     scanf("%d",value2);   if (*value2 < *value1)     {       printf("\nError! Enter a value >= %d. \n",*value1);     } }while(*value2 < *value1); return; } int numbers(int value1, int value2) {   int value;   int counter;  while (value1<value2);     value1++;     counter++;   {   if (value1%counter == 0)   }   while (value1%counter != 0);     fprintf("\n %d", value)   return; }```
• 03-27-2011
smokeyangel
How I would do it (and this is independent of the coding side of times) would be

Code:

```for each number i in range     for each integer j between 1 and i           does j divide i? (and j is not 1 and not i)           if so, set a flag     if no divisors found i is prime           print i```
More efficient ways would be to only loop up to i/2 when looking for divisors, and to break out of the inner for loop when a divisor is found.

The function returns an int -- I guess that's the number of primes in the range? So you need to keep track of how many primes you've found -- do this in the outer for loop.

Coding wise.... I don't think there's anything too nasty here. You can use (num%div) == 0 to determine if a number divides exactly by another (looks like you already know this).

Hopefully that's not too vague. Trying to avoid just giving you the answer so you'll still learn something from doing it.
• 03-27-2011
dvsumosize
look up the modulus command, you can do a FOR LOOP instead if you don't want to do modulus, having that loop going from 1 to 1/2 of the number you are checking for.

Basically to check to see if it is a prime number you see if it ever is divisible by another number * that number = itself then its not a prime.

i.e. 4/3 * 3 =/= 4, but 4/2 * 2 = 4 so its not a prime. You have been taught about integer division right?
• 03-28-2011
iMalc
Code:

` while (value1<value2);`
That's what you call an infinite loop waiting to happen. I'd fix that before your CPU gets hurt.:p
• 03-28-2011
GReaper
Yeah, better and pretty faster too!
• 03-28-2011
cubbies5
Thanks for the replies.
I find smokeyangels code to be a little confusing/vague sorry. Flag??
so the more efficient code would be:
Code:

```int numbers(int i, int j) {   for i++     for 1<j++ && j++<i       j/i       printf("%d",i); return(i); }```
I've tried a different method as well if you think this may be better let me know please.
Code:

``` int numbers(int value1, int value2) {   int div=2;   while (value1<=value2);   int  increase=1;     while (div<=value1 && value1%div !=0);     increase++;     div++;     printf("%d",value1); return(value1); }```
• 03-28-2011
GReaper
Code:

`while (value1<=value2);`
This will be an infinite loop if "value1 <= value2"!
• 03-28-2011
cubbies5
So in order to prevent that infinite loop what should I do instead?
• 03-28-2011
GReaper
Quote:

Originally Posted by cubbies5
So in order to prevent that infinite loop what should I do instead?

Maybe you should add some curly braces like:
Code:

```int numbers(int value1, int value2) {   int div=2;   while (value1<=value2)   {     int  increase=1;     while (div<=value1 && value1%div !=0)     {         increase++;         div++;     }     printf("%d",value1);   }   return(value1); }```
It's inefficient and ugly, but i think it works. Well done!
• 03-28-2011
cubbies5
Unfortunately it doesn't work :-/
All i can get it to print are the boundaries
• 03-28-2011
cubbies5
This is the new code i've developed but the problem im having is that it only prints the bounds inputted, 0, and 1. Any suggestions?
Code:

```#include <stdio.h> void getinput(int*,int*); void numbers(int, int); int main() {   int value1;   int value2;   getinput(&value1, &value2);   numbers(value1,value2);   printf("%d, %d \n",value1, value2);   return(0); } void getinput(int* value1,int* value2) {   do   {     printf("Enter starting range value:  ");     scanf("%d",value1);     if (*value1 < 0)     {       printf("\nError! Non-negative integers only!\n");     }   }   while(*value1 <  0);   do   {     printf("Enter ending range value: ");     scanf("%d",value2);     if (*value2 < *value1)     {       printf("\nError! Enter a value >= %d. \n",*value1);     }   }while(*value2 < *value1);   return; } void numbers(int value1, int value2) {   int div; int increase;   div=2;   increase=2;   while (value1<=value2)   {     while (div<=value1)     {       if ((value1%div) == 0)       {         increase = 0;         div++;       }       else       {         increase++;         div++;       }     }     if (increase == div)     {       printf("%d\n", value1);     }     value1++;     div = 2;     increase = 2;   } }```
• 03-30-2011
GReaper
I see two mistakes in there:
1) You keep on looping even after you find out that the number isn't prime
2) Your line "if (increase == div)" is logically wrong! Try something like:
Code:

```      if ((value1%div) == 0)       {         increase = -1;         break;       }           .......     if (increase != -1)         .......```