#include <stdio.h>
#include <math.h>
int fib();
int scan;
int main() {
scanf("%d", &scan);
printf("%d\n", fib());
scanf("%s");
return 0;
}
int fib() {
return floor((pow(1+sqrt(5)/2, scan)-(-pow(1-sqrt(5)/2, scan)))/sqrt(5));
}
I'm pretty new to programming with C and decided to try and calculate any number in the Fibonacci series. I based it off of my lua script here. I'm at a loss of what I've done wrong, could someone give me some insight?
You have the formula wrong. You want fib to be:
int fib() {
return round((pow((1+sqrt(5))/2, scan)-(-pow((1-sqrt(5))/2, scan)))/sqrt(5));
}
instead. You were missing parenthesis around the 1+sqrt(5) and 1-sqrt(5) terms and were using floor instead of round, which was underestimating the fibonacci numbers in my tests. (This mostly has to do with low precision in the pow function. The seventh fibonacci number, 13, came out to 12.969)
You also probably want to change
scanf("%s");
to
char tmp;
scanf("%c", &tmp);
Since the way you have it incorrectly omits an argument.
Hope this helps!
Related
Note: I'm fairly new to C programming so I don't know everything just yet.
So I'm working on this assignment for my programming class where I have to write a recursive function count_digits( ) that counts all the digits in a string. I wrote the program and got it to compile but when I type in a number, it always gives me the same answer.
This is what my code is:
#include <stdio.h>
int count_digits(int num)
{
static int count=0;
if(num>0)
{
count++;
count_digits(num/10);
}
else
{
return count;
}
}
int main()
{
int number;
int count=0;
printf("Enter any number:");
scanf("%d",&number);
count=count_digits(number);
printf("\nTotal digits in [%d] are: %d\n",number,count);
return 0;
}
Your non void function returns nothing if num is greater than zero. The compiler should warn you about not returning value. The fix:
return count_digits(num/10);
there are a few things to consider:
What happens if you call your function count_digit() more than one time in the program?
What if you enter 0, 10, 100 as number?
Perhaps you should rethink using a static variable here.
Also for debugging, insert some printfs (or use the debugger) in count_digit() to check how your function behaves.
I am having a go at Project Euler question 4 - Finding the highest palindrome from a product of two 3-digit numbers. I have found many solutions online, none of which answer the issues I am experiencing with my own code. I am not an experienced coder and have just starter learning recreationally.
My code so far does a for loop which generates all of the numbers that are palindromes. Currently I have it printing all of them just to check that it is working, which it appears to be. I was wondering if there was a way to print only the largest number in the loop? I don't think running through manually is the most efficient way of doing it.
Here is the code so far:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
int main()
{
int a,b,c,d;
for(a=100;a<=999;a++){
for(b=100;b<=999;b++){
c=a*b;
d=reverse(c);
if(c==d){
printf("%d is a palindrome\n",c);
}
}
}
return 0;
}
int reverse(int number){
int answer=0;
while(number!=0){
int units=number%10;
answer=answer*10+units;
number=number/10;
}
return answer;
}
Assign the largest result to a variable. Then when you find another that is bigger, update that variable.
Then when the loop is complete, print the result from the variable which is the largest.
I'll let you work out the code. It's trivial.
OK, so my task is to get a single digit from a natural number and sum the square numbers (Using function while, which means no arrays yet :S). For instance I type 123 so sum=1*100+2*10+3*1; However the problem is that the digit could be whatever. My problem is that the power rises with int but its like so - 1, 10, 99, 1000. The problem for me is 99. Also answer is looping but I'll fix it later. Here is my code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
int N,
number=0,
answer=0,
a=1,
i=0;
printf("Type natural number: ");
scanf("%d",&N);
while(N>i)
{
number=N%10;
N/=10;
a=10;
a=pow(a,i);
answer+=number*number*a;
printf("%d\n", answer);
i++;
}
return 0;
}
Try it the other way around. Don't make the input an integer. Start at the beginning of the stream, get the character, convert it to an int 'number'. Then do
answer = 10 * answer;
answer += (number * number);
This will build up your answer little by little. Note that I am not sure that this is what you are asking for due to your example not seeming to match the code.
Let me know if this is off-base and I will update it.
Create a program to find out the first perfect square greater than 1 that occurs in the Fibonacci sequence and display it to the console.
I have no output when I enter an input.
#include <stdio.h>
#include <math.h>
int PerfectSquare(int n);
int Fibonacci(int n);
main()
{
int i;
int number=0;
int fibNumber=0;
int psNumber=0;
printf("Enter fibonacci number:");
scanf("%i",&number);
fibNumber = Fibonacci(number);
psNumber = PerfectSquare(fibNumber);
if(psNumber != 0){
printf("%i\n",psNumber);
}
}
int PerfectSquare(int n)
{
float root = sqrt(n);
if (n == ((int) root)*((int) root))
return root;
else
return 0;
}
int Fibonacci(int n){
if (n==0) return 0;
if (n==1) return 1;
return( Fibonacci(n-1)+Fibonacci(n-2) );
}
Luke is right. If your input is n, then the Fibonacci(n) returns the (n+1)th Fibonacci number.
Your program check whether (number +1)th is perfect square or not actually.
If you enter 12, then there is output. Because the 13th Fibonacci number is 144. And it is perfect square. PS: print fibNumber instead of psNumber.
printf("%i\n", fibNumber);
Right now you're only calculating one Fibonacci number and then testing whether it's a perfect square. To do this correctly you'll have to use a loop.
First suggestion is to get rid of the recursion to create fib numbers. You can use 2 variables and continually track the last 2 fib numbers. They get added something like:
fib1=0;fib2=1;
for(i=3;i<MAXTOCHECK;i++)
{
if(fib1<fib2)
fib1+=fib2;
else
fib2+=fib1;
}
What is nice about this method is that first you can change you seeds to anything you want. This is nice to find fib like sequences. For example Lucas numbers are seeded with 2 and 1. Second, you can put your check for square inline and not completely recalculate the sequence each time.
NOTE: As previously mentioned, your index may be off. There is some arbitrariness of indexing fib numbers from how it is initially seeded. This can seen if you reseed with 1 and 1. You get the same sequence shifted by 1 index. So be sure that you use a consistent definition for indexing the sequence.
So here is a program that is supposed to calculate an approximation to the value of pi if you take enough terms into the sum which is mathematically described in the following program and calculates the expression of the root, you get a value that gets closer and closer to the value of pi the more terms you have.
#include <stdio.h>
#include <math.h>
main()
{
int j, terms;
double sum, precision, pi;
printf("How many terms: "); scanf("%d", &terms);
for(j=1;j<=terms;j++)
sum+=1/(j*j);
pi = sqrt(6*sum);
printf("Pi: %lf.\n", pi);
}
But there is something making it go wrong here and I can't quite figure out what.
sum+=1/(j*j);
I thought the mistake might be in that line because all others look fine,thinking at first maybe the computer isn't counting decimals.I'm unsure.But my question is: What is it in this code that makes it malfunction?And how do I fix it?
This performs integer division:
1/(j*j);
try this:
sum+=1.0/(j*j);
If j*j might overflow, do this
sum+=1.0/((double)j*j);