# Thread: is my work correct?...simple function

1. ## is my work correct?...simple function

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);

}```

2. You need a simple case to stop the recursion.
Like
if ( n <= 1 ) return n;

3. You need something like this:
Code:
```if (n <= 1)
{
return 1;
}```
If you want to stop it at the logical point. Thus the function would be written as this:
Code:
```int fibo(int n)
{
if (n <= 1)
{
return 1;
}
return fibo(n-1) + fibo(n-2);
}```
Also, keep in mind that this is zero based, thus fibo(5) returns 8, not 5.

4. 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:

Code:
```int isPrime (int n)
{

if (n%2 != 0)

return 1;

else

return 0;

}```
2. a recursive function int fibo (int n) that returns the nth fibonacci number

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:

Code:
```int isFiboPrime (int n)
{

if ( fibo(n) ! isPrime(n) )
printf ("Not Prime");

else

printf ("Prime");

}```
is this correct?

5. 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;
}
}
}```

6. 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?

7. Try this:
Code:
```int isFiboPrime (int n)
{
int temp = fibo(n);
if (isPrime(temp) == 1)
return 1;
else
return 0;
}```
edit: sorry, you wanted to printf:
Code:
```void  isFiboPrime (int n)
{
int temp = fibo(n);
if (isPrime(temp) == 1)
printf ("Prime");
else
printf ("Not Prime");
}```

8. 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...

9. 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.

10. ok thans...gonna check this out....

thanks for the corrections...

really thanks

11. That's what we're here for!

12. 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 */```

13. 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;
}```
The text passage contains multiple programming-related questions and discussions, but no explicit mathematical questions or calculations are presented. The content revolves around the correctness and efficiency of code for generating Fibonacci numbers and checking for prime numbers.
no_question
Given a text passage, determine:
    1. If it contains a mathematical question (including word problems or calculations)
    2. If an answer to that mathematical question is provided in the text

First, describe the content structure you found in 1-2 sentences.

Then provide your analysis in the following format:
TYPE: <one of the following values>
- NO_QUESTION
- SINGLE_QUESTION
- SINGLE_QUESTION_WITH_ANSWER
- MULTIPLE_QUESTIONS
- MULTIPLE_QUESTIONS_WITH_ANSWERS
system
# Thread: is my work correct?...simple function

1. ## is my work correct?...simple function

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);

}```

2. You need a simple case to stop the recursion.
Like
if ( n <= 1 ) return n;

3. You need something like this:
Code:
```if (n <= 1)
{
return 1;
}```
If you want to stop it at the logical point. Thus the function would be written as this:
Code:
```int fibo(int n)
{
if (n <= 1)
{
return 1;
}
return fibo(n-1) + fibo(n-2);
}```
Also, keep in mind that this is zero based, thus fibo(5) returns 8, not 5.

4. 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:

Code:
```int isPrime (int n)
{

if (n%2 != 0)

return 1;

else

return 0;

}```
2. a recursive function int fibo (int n) that returns the nth fibonacci number

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:

Code:
```int isFiboPrime (int n)
{

if ( fibo(n) ! isPrime(n) )
printf ("Not Prime");

else

printf ("Prime");

}```
is this correct?

5. 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;
}
}
}```

6. 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?

7. Try this:
Code:
```int isFiboPrime (int n)
{
int temp = fibo(n);
if (isPrime(temp) == 1)
return 1;
else
return 0;
}```
edit: sorry, you wanted to printf:
Code:
```void  isFiboPrime (int n)
{
int temp = fibo(n);
if (isPrime(temp) == 1)
printf ("Prime");
else
printf ("Not Prime");
}```

8. 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...

9. 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.

10. ok thans...gonna check this out....

thanks for the corrections...

really thanks

11. That's what we're here for!

12. 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 */```

13. 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;
}```
user
The text passage contains multiple programming-related questions and discussions, but no explicit mathematical questions or calculations are presented. The content revolves around the correctness and efficiency of code for generating Fibonacci numbers and checking for prime numbers.

TYPE: NO_QUESTION