Help with square root function (in C)
Author |
Message |
marie85
|
Posted: Tue Oct 31, 2006 8:51 pm Post subject: Help with square root function (in C) |
|
|
Hi,
I'm new to ComSci.ca but have been reading here for quite a while. I'm also a bit new to the world of computer science(not professional level yet though, still in HS lol) and there are many things that I have me a little confused.
Well, I'm doing some excersice programs, and I need to write a function that will display all the numbers of the fibonaci sequence that are smaller than 500 and have a whole sqare root. Also, the function must display how many numbers have a whole square root (so, if only 1 number has a whole square root then display "1").
I'm trying my best to understand all this, but I seem to be having lots of problems with programming. I'll keep trying, and I hope you guys can help me out here. Thanks you so much in advance |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
Andy
|
Posted: Tue Oct 31, 2006 9:32 pm Post subject: (No subject) |
|
|
Welcome! the hardest thing i find about CS is how to break the problem down into smaller solvable pieces.
I digress
You probably know how to, write a program that will generate the fibonacci sequence.
So write a functio nthat will test whether if a number is a perfect square.
then simply run the function on each of the element of the sequence, and add 1 to a counter if your function returns true.
wrap everything up in a for loop of 500, and you're done!
If you need more help, post some of your attempts so we can give you proper guidelines. |
|
|
|
|
![](images/spacer.gif) |
marie85
|
Posted: Tue Oct 31, 2006 9:51 pm Post subject: (No subject) |
|
|
This is what I've been trying to do, but it doesn't seem to work when I include it in the program
code: |
whole (int f);
{
int x=0,y=1, f;
for (f= 0; f<500)
{ x = f;
f = x + y;
y = x; }
if (sqrt(f)*sqrt(f)==f)
return (1);
else
return (0);
}
|
I know it's probably all wrong, but I don't know what else to do. I'm determined to pass this course (I must, since it's too late to drop it lol).
Thanks ![Smile Smile](http://compsci.ca/v3/images/smiles/icon_smile.gif) |
|
|
|
|
![](images/spacer.gif) |
md
![](http://compsci.ca/v3/uploads/user_avatars/1849317514ed6c4399768d.png)
|
Posted: Tue Oct 31, 2006 10:37 pm Post subject: (No subject) |
|
|
since the 500th's fibonnaci number isn't that big; just calculate all the perfect squares that are smaler then it, and all the fibbonacy numbers; compare and ouput the same.
psudocode
code: |
fib1 = 1, fib2 = 1, nat = 1, tmp, count = 1
while n < 500
count += 1
if fib2 > nat*nat then
tmp = fib1 + fib2, fib1 = fib2, fib2 = tmp
else if nat*nat > fib2 then
nat += 1
else if nat * nat = fib2 then
output fib2 + "is a perfect suare, and is the " + count + " number in the fib. squence"
|
|
|
|
|
|
![](images/spacer.gif) |
md
![](http://compsci.ca/v3/uploads/user_avatars/1849317514ed6c4399768d.png)
|
Posted: Tue Oct 31, 2006 10:39 pm Post subject: (No subject) |
|
|
[edit type followup] replace n in the loop with count, and make the >'s < |
|
|
|
|
![](images/spacer.gif) |
|
|