a recursive function int fibo(int n) that returns the nth fibonacci number.
i dont really quite get this..so here goes
Code:int fibo (int n) { return fibo(n-1) + fibo(n-2); }
a recursive function int fibo(int n) that returns the nth fibonacci number.
i dont really quite get this..so here goes
Code:int fibo (int n) { return fibo(n-1) + fibo(n-2); }
You need a simple case to stop the recursion.
Like
if ( n <= 1 ) return n;
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
You need something like this:If you want to stop it at the logical point. Thus the function would be written as this:Code:if (n <= 1) { return 1; }
Also, keep in mind that this is zero based, thus fibo(5) returns 8, not 5.Code:int fibo(int n) { if (n <= 1) { return 1; } return fibo(n-1) + fibo(n-2); }
Edit: Sorry Salem, didn't see your reply
thanks guys...i understand now
but need to check if my last code is correct..pls read first
1.function int isPrime(int n ) that returns 1 if n is a prime and 0 otherwise.
done this already but if needs any corrections here it is:
2. a recursive function int fibo (int n) that returns the nth fibonacci numberCode:int isPrime (int n) { if (n%2 != 0) return 1; else return 0; }
done this already...thanks for the corrections!
3. A function int isFiboPrime (int n) that checks whether the nth fibonacci number is a prime.
This function must call the 1 & 2 functions
so heres my code:
is this correct?Code:int isFiboPrime (int n) { if ( fibo(n) ! isPrime(n) ) printf ("Not Prime"); else printf ("Prime"); }
isPrime doesn't check for, say, division by three. You need to check for division probably up to half the number. For example, check if 21 is prime. Your function says it is, but if you check for three, you'll realize it's not. In code:Code:int isPrime (int n) { for (int i = 2; i <= (n/2); i++) { if (n%i == 0) { return 0; } } }
hmmm i see so it was only for division by 2...i didnt know that...thanks for the correction
but how about my third function...is it correct?
Last edited by enjoyincubus; 11-09-2006 at 07:02 AM.
Try this:edit: sorry, you wanted to printf:Code:int isFiboPrime (int n) { int temp = fibo(n); if (isPrime(temp) == 1) return 1; else return 0; }Code:void isFiboPrime (int n) { int temp = fibo(n); if (isPrime(temp) == 1) printf ("Prime"); else printf ("Not Prime"); }
Last edited by manutd; 11-09-2006 at 07:12 AM.
aaww..i didnt thought of this before...i guess i need more practice
btw will my answer still works?...
thanks for corrections
EDIT: OT, a manchester united fan?...hehe im going for chelsea this season...
I don't think so. It will check isPrime, but I don't think (I could be wrong) it would take into acount the fibo(n) call. Also, my method is clearer and makes for more readable code.
ok thans...gonna check this out....
thanks for the corrections...
really thanks
That's what we're here for!
If no number from 2 to sqrt(n) can divide evenly into n, then n is prime.
Code:for test = 2; test * test < n; ++test { if n divisible by test /* not prime, return failure */ } /* n passed all tests, return success */
Code:#include <stdio.h> void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){ puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9 /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i] ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][ t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}
I would like to make a small modification to the last post on calculating primes to improve its efficieny. Trying to divide n only by numbers between 2 to sqrt(n) reduces a lot of work, however we can reduce that too, by half if we skip all even numbers other than 2.
Code:int isPrime (int n) { if (n == 2) return 1; if( n % 2 == 0) return 0; // all even numbers except 2 are non primes for (int i = 3; i * i <n; i+=2 ) //check only with odd numbers less than sqrt(n) { if ( n % i == 0) return 0; } return 1; }