Thread: Prime numbers

  1. #1
    Registered User
    Join Date
    Mar 2011
    Location
    Chicago
    Posts
    5

    Question 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;
    }

  2. #2
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    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.

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    24
    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?

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Code:
     while (value1<value2);
    That's what you call an infinite loop waiting to happen. I'd fix that before your CPU gets hurt.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Yeah, better and pretty faster too!
    Devoted my life to programming...

  6. #6
    Registered User
    Join Date
    Mar 2011
    Location
    Chicago
    Posts
    5
    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);
    }
    Last edited by cubbies5; 03-28-2011 at 02:03 PM.

  7. #7
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Code:
    while (value1<=value2);
    This will be an infinite loop if "value1 <= value2"!
    Devoted my life to programming...

  8. #8
    Registered User
    Join Date
    Mar 2011
    Location
    Chicago
    Posts
    5
    So in order to prevent that infinite loop what should I do instead?

  9. #9
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Quote Originally Posted by cubbies5 View Post
    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!
    Last edited by GReaper; 03-28-2011 at 02:39 PM.
    Devoted my life to programming...

  10. #10
    Registered User
    Join Date
    Mar 2011
    Location
    Chicago
    Posts
    5
    Unfortunately it doesn't work :-/
    All i can get it to print are the boundaries

  11. #11
    Registered User
    Join Date
    Mar 2011
    Location
    Chicago
    Posts
    5
    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;
      }
    }

  12. #12
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    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)
        
        .......
    Devoted my life to programming...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Prime numbers program
    By Prestige in forum C Programming
    Replies: 3
    Last Post: 10-16-2010, 01:57 PM
  2. prime numbers and time consuming
    By Gustaff in forum C Programming
    Replies: 21
    Last Post: 08-30-2010, 01:29 PM
  3. Recuration Prime Numbers
    By megazord in forum C Programming
    Replies: 17
    Last Post: 05-17-2010, 08:56 AM
  4. Perfect and Prime numbers
    By taggrath in forum C++ Programming
    Replies: 3
    Last Post: 10-22-2009, 02:13 AM
  5. Replies: 18
    Last Post: 11-04-2005, 02:41 PM

Tags for this Thread