Thread: Prime

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    28

    Prime

    Computing teacher gave me homework to make a program which checks if a number is prime i wrote this:
    Code:
    int asal(int a,int b){
    	if(b=1){return 2;}
    	else if(a%b!=0){return asal(a,b-1);}
    	else if(a%b==0){return 1;}}
    
    int main(){
    	if(asal(27,26)==2){printf(" is a prime");}
    	if(asal(27,26)==1){printf(" isn't a prime");}
    	getchar();
    	return 0;}
    it returns "its a prime" where is the problem????

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Try adding a printf() to the beginning of asal() to show the value of a and b respectively.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why does your function that checks for prime numbers require two inputs? That's just odd.


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Uh, your function returns either 1 or 2, both of which mean "true". Perhaps use 0 for false.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Nope. The problem is just "if (b==1)"

    Also I guess it need not be called twice...hmm.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    Another suggestion would be to work off existing code that specializes in figuring out/calculating prime numbers which will be the openssl source code package. If you are fond at reviewing source code this would be a good place to seek out how efficient code is written. A great learning tool IMHO.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It makes more sense to just type "prime" in the search box here, than it does to sort through the source code for openssl...


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Calculating prime factor of a huge number.
    By Bakster in forum C Programming
    Replies: 15
    Last Post: 02-20-2009, 12:06 PM
  2. prime number program with function
    By mackieinva in forum C Programming
    Replies: 17
    Last Post: 09-20-2007, 08:36 AM
  3. prime numbers, counters, help!
    By vege^ in forum C++ Programming
    Replies: 1
    Last Post: 03-10-2003, 04:32 PM
  4. Prime Wonder
    By vasanth in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 10-07-2002, 11:49 PM
  5. Homework help
    By Jigsaw in forum C++ Programming
    Replies: 2
    Last Post: 03-06-2002, 05:56 PM