Thread: Prime numbers

Hybrid View

Previous Post Previous Post   Next Post Next Post
  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
    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...

  5. #5
    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"

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