Thread: Prime Checking Algorithm

  1. #1
    Registered User
    Join Date
    Jun 2007
    Location
    United States
    Posts
    6

    Prime Checking Algorithm

    I'm still new to C. I wrote some code, but I'm sure I did something wrong because it's not giving the correct output.

    (compiling and running on linux)

    Code:
    void main()
    {
    	unsigned long startingNumber; // THE Number. Is it prime?
    
    	printf("Enter Positive Number (0 to 4,294,967,295) to be Tested : ");
    	scanf(startingNumber);
    	printf("\nTesting ", startingNumber, "...\n");
    
    	unsigned long testNumber = (startingNumber / 2);
    	// A number can't be evenly divided by a number bigger than half the first number.
    
    	unsigned long answer = startingNumber % testNumber; // Get the initial remainder.
    	
    	while (answer != 0 && testNumber > 1) // Decrement while checking for a modulus of 0.
    	{
    		answer = startingNumber % testNumber;
    		testNumber = testNumber - 1;
    	}
    
    	if ((answer == 0) && ((startingNumber / 2) > 1) || (startingNumber == 2))
    	{
    		// Check for prime, with fixes for 2, and testNumbers 1 or smaller.
    		printf(startingNumber, " is NOT a PRIME Number\n");
    	}
    	else
    		printf(startingNumber, " is a PRIME Number\n");
    	return 0;
    };

    Output:

    Code:
    fuzzypig@fuzzypig-ubuntu:~/Documents/source/primechk/c$ ./a.out
    Enter Positive Number (0 to 4,294,967,295) to be Tested : 265952  
    
    Testing Hfῼロ�fuzzypig@fuzzypig-ubuntu:~/Documents/source/primechk/c$
    Using GDB, the program exits with code 011.
    Can someone help me fix my code?

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    I'm surprised that even compiles.

  3. #3
    Registered User
    Join Date
    Jun 2007
    Location
    United States
    Posts
    6
    Well thanks, that helps SO much.
    What mistakes did I make?
    (other than forgetting to convert startingNumber with atoi)
    Last edited by fuzzypig; 06-21-2007 at 05:02 PM.

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Well, I was going to point out a few things, and then I realized the list is kind of big..... Since you insist.... I'll even be somewhat "verbose" about it.

    • "void main()" should be "int main(void)".
    • After printf(), the text might not show up because you didn't include a '\n', which is fine, but you might want to do a "fflush(stdout);"
    • "scanf(startingNumber);" is totally wrong. Consult the man pages or google for info on scanf().
    • "printf("\nTesting ", startingNumber, "...\n");" <------- again, totally wrong. Consult the man pages or google for info on printf().
    • You are declaring variables in between your code. This is allowed in C++ and C99, but not in C89/C90.
    • You are using // as comments, which are generally accepted in C, but are actually not really a part of C89/C90, but are in C++ and C99.
    • Be careful with your conditions... Putting parenthesis around conditions help to make the code more readable and protects you from operator precedence that you might not expect.
    • You somehow managed to declare main() as void, but return an int anyway. That should have been picked up imo. Returning the int is good, but declare main() as returning it!


    Not all of these are actual error messages per se, but just also good programming tips.

  5. #5
    Registered User
    Join Date
    Jun 2007
    Location
    United States
    Posts
    6
    I tried to follow that, but it's still not working.

    New code:
    Code:
    int main(void)
    {
    	unsigned long startingNumber; // THE Number. Is it prime?
    	unsigned long testNumber;
    	unsigned long answer;
    
    	printf("Enter Positive Number (0 to 4,294,967,295) to be Tested : \n");
    	scanf("%i", &startingNumber);
    	printf("Testing \n", startingNumber);
    
    	testNumber = (startingNumber / 2);
    	// A number can't be evenly divided by a number bigger than half the first number.
    
    	answer = startingNumber % testNumber; // Get the initial remainder.
    	
    	while (answer != 0 && testNumber > 1) // Decrement while checking for a modulus of 0.
    	{
    		answer = startingNumber % testNumber;
    		testNumber = testNumber - 1;
    	};
    
    	if ((answer == 0) && ((startingNumber / 2) > 1) || (startingNumber == 2))
    	{
    		// Check for prime, with fixes for 2, and testNumbers 1 or smaller.
    		printf(startingNumber, " is NOT a PRIME Number\n");
    	};
    	else
    		printf(startingNumber, " is a PRIME Number\n");
    	return 0;
    };
    I still get several compiler warnings:
    Code:
    primechk.c: In function ‘main’:
    primechk.c:9: warning: incompatible implicit declaration of built-in function ‘printf’
    primechk.c:10: warning: incompatible implicit declaration of built-in function ‘scanf’
    primechk.c:10: warning: passing argument 1 of ‘scanf’ makes pointer from integer without a cast
    primechk.c:27: warning: passing argument 1 of ‘printf’ makes pointer from integer without a cast
    primechk.c:30: warning: passing argument 1 of ‘printf’ makes pointer from integer without a cast
    primechk.c:31: warning: ‘return’ with a value, in function returning void
    primechk.c:6: warning: return type of ‘main’ is not ‘int’

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    unsigned long startingNumber;
    scanf("&#37;i", &startingNumber);
    unsigned longs should be read with the format specifier %lu.
    Code:
    printf("Testing \n", startingNumber);
    There's no format specifier for startingNumber. That should be
    Code:
    printf("Testing %lu\n", startingNumber);
    This
    Code:
    printf(startingNumber, " is NOT a PRIME Number\n");
    should, again, be
    Code:
    printf("%lu is NOT a PRIME Number\n", startingNumber);
    Semicolons are unnessesary after (most) closing braces: '}'.

    [edit]
    primechk.c:31: warning: ‘return’ with a value, in function returning void
    primechk.c:6: warning: return type of ‘main’ is not ‘int’
    Apparently your main function is still returning void. Try saving your source file. [/edit]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    BTW, don't put a ; at the end of a function definition.

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    I said that. At the ends of ifs and whiles too. At any closing brace ('}') except for structures and initializer lists.

    Well, actually, there's nothing wrong with doing so. It's just spurious and annoying for other programmers to read -- they think they're looking at the end of a struct or something.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    LOL, I read where you said that about blocks, and didn't realize you were talking about main(). Thought you were just referrring to ones around any given if or while statement or something.

    Must....learn....to....read.....

  10. #10
    Registered User
    Join Date
    Jun 2007
    Location
    Lees Summit Missouri
    Posts
    11
    It's cool that fuzzypig also uses ubuntu !
    It's rather ideal for gcc.

    I've written prime programs before but I really better not post them.

  11. #11
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Methinks someone needs to use the -Werror flag and stop ignoring warnings!
    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"

  12. #12
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by honorable_sir View Post
    It's cool that fuzzypig also uses ubuntu !
    It's rather ideal for gcc.
    Strange thing to say. I'd say ANY Linux, or for that matter any platform where gcc works, is "ideal for gcc"

  13. #13
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You can stop checking whether the number is prime or not after testing up to, and including, the square root of the number. If it's still prime at that point, then it will stay prime all the way.

    Big speed up if you're trying to find lots of prime numbers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. algorithm for duplicate file checking help
    By geekoftheweek in forum C Programming
    Replies: 1
    Last Post: 04-04-2009, 01:46 PM
  2. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 09:38 AM
  3. Problems about gcc installation
    By kevin_cat in forum Linux Programming
    Replies: 4
    Last Post: 08-09-2005, 09:05 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. Fast prime determining algorithm
    By Ken Fitlike in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 12-01-2002, 03:05 AM