473,686 Members | 2,162 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Fibonacci...

Im actually kinda embarassed to ask this question...

@code start
#include <iostream>
int main() {
unsigned long long a = 1;
unsigned long long b = 1;
for (int i = 0; i < 45; i++) {
a += b;
std::cout << a/b << std::endl;
b += a;
std::cout << b/a << std::endl;
}
std::cin.get();
}
@code end

Why cant i get it to work?

Regards
Zacariaz

Feb 17 '06 #1
14 4935
Guess this would be more correct:

@code start
#include <iostream>
int main() {
unsigned long long a = 1;
unsigned long long b = 1;
std::cout << b/a << std::endl;
for (int i = 0; i < 91; i++) {
if (a > b) {
b += a;
std::cout << b/a << std::endl;
}
else if (b >= a) {
a += b;
std::cout << a/b << std::endl;
}
}
std::cin.get();
}
@code end

Feb 17 '06 #2
fe**********@ho tmail.com wrote:
Im actually kinda embarassed to ask this question...

@code start
#include <iostream>
int main() {
unsigned long long a = 1;
unsigned long long b = 1;
for (int i = 0; i < 45; i++) {
a += b;
std::cout << a/b << std::endl;
b += a;
std::cout << b/a << std::endl;
}
std::cin.get();
}
@code end

Why cant i get it to work?

Do you really want to know the answer? I mean, don't you actually
already know the answer?..

What do you expect from your program? What seems to be the problem?

V
--
Please remove capital As from my address when replying by mail
Feb 17 '06 #3
fe**********@ho tmail.com wrote:
Guess this would be more correct:
"More correct"? You mean, it actually does what you need? Or
does it just _seem_ to be better?
@code start
#include <iostream>
int main() {
unsigned long long a = 1;
unsigned long long b = 1;
std::cout << b/a << std::endl;
for (int i = 0; i < 91; i++) {
if (a > b) {
b += a;
std::cout << b/a << std::endl;
}
else if (b >= a) {
a += b;
std::cout << a/b << std::endl;
}
}
std::cin.get();
}
@code end


V
--
Please remove capital As from my address when replying by mail
Feb 17 '06 #4
I whant to end up with the number 1.618, or something like it, but i
have trued averything it seemes, it will only print integers, i should
know why, but either i've forgotten how this work (aint working alot
with float/double) or im just stupid.

If you remove the /a and /b from the code u'll see that it does indeed
work, but those are not the numbers i want.

Feb 17 '06 #5
In article <11************ **********@f14g 2000cwb.googleg roups.com>,
fe**********@ho tmail.com wrote:
Im actually kinda embarassed to ask this question...

@code start
#include <iostream>
int main() {
unsigned long long a = 1;
unsigned long long b = 1;
for (int i = 0; i < 45; i++) {
a += b;
std::cout << a/b << std::endl;
b += a;
std::cout << b/a << std::endl;
}
std::cin.get();
}
@code end

Why cant i get it to work?


What output were you expecting? If you wanted it to output the Fibonacci
sequence, I'd say you can't get it to work because you are using the
wrong algorithm. :-)

Here is a recursive algorithm:

int fibonacci( int i ) {
if ( i == 0 ) return 0;
if ( i == 1 ) return 1;
else return fibonacci( i - 1 ) + fibonacci( i - 2 );
}

And non-recursive:

int fibonacci( int i ) {
int result[] = { 0, 1 };
while ( i > 1 ) {
int t = result[0];
result[0] = result[1];
result[1] = result[0] + t;
--i;
}
return result[i];
}

int main() {
for ( int i = 0; i < 45; ++i )
cout << i << ": " << fibonacci( i ) << '\n';
}

--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
Feb 17 '06 #6
fe**********@ho tmail.com wrote:
I whant to end up with the number 1.618, or something like it, but i
have trued averything it seemes, it will only print integers, i should
know why, but either i've forgotten how this work (aint working alot
with float/double) or im just stupid.

If you remove the /a and /b from the code u'll see that it does indeed
work, but those are not the numbers i want.


When you divide a integer by an integer, you get an integer. What you
want is to convert one of them to double before dividing, like
double(a)/b

V
--
Please remove capital As from my address when replying by mail
Feb 17 '06 #7
Hey im thank full for your input, but it work, dont say otherwise.
I have tryed casting, but then i casted both numbers, maybe this will
work, if it does thanks

Feb 17 '06 #8
* Daniel T.:
* fe**********@ho tmail.com:
Im actually kinda embarassed to ask this question...

@code start
#include <iostream>
int main() {
unsigned long long a = 1;
unsigned long long b = 1;
for (int i = 0; i < 45; i++) {
a += b;
std::cout << a/b << std::endl;
b += a;
std::cout << b/a << std::endl;
}
std::cin.get();
}
@code end

Why cant i get it to work?


What output were you expecting?


It seems he wants a series converging (slowly) to the golden ratio, or
something like that.

The problem is that he performs an integer division instead of a
floating point division.

Here are some ways of forcing a floating point division in C++:

double(a)/b instead of a/b
1.0*a/b instead of a/b
(a+0.0)/b instead of a/b

double a instead of unsigned long long a;

We should note, however, that the above is not a standard C++ program
since there is no "long long" type in C++.

C++ does have a "long double" type.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Feb 17 '06 #9

Daniel T. wrote:
Here is a recursive algorithm:

int fibonacci( int i ) {
if ( i == 0 ) return 0;
if ( i == 1 ) return 1;
else return fibonacci( i - 1 ) + fibonacci( i - 2 );
}

And non-recursive:

int fibonacci( int i ) {
int result[] = { 0, 1 };
while ( i > 1 ) {
int t = result[0];
result[0] = result[1];
result[1] = result[0] + t;
--i;
}
return result[i];
}

int main() {
for ( int i = 0; i < 45; ++i )
cout << i << ": " << fibonacci( i ) << '\n';
}


FWIW I justput up an O(1) solution on comp.programmin g. Here it is:

/*
run time fibonacci
*/
//Boost.Preproces sor library
// available at http://wwww.boost.org
#include <boost/preprocessor/repetition.hpp>
#include <boost/preprocessor/control/if.hpp>
#include <boost/preprocessor/comparison/equal.hpp>
#include <boost/preprocessor/empty.hpp>
#include <boost/preprocessor/comma.hpp>
#include <boost/preprocessor/arithmetic/add.hpp>

#include <iostream>
#include <stdexcept>

template<int N>
struct fibonacci{
static const int value
= fibonacci<N-1>::value
+ fibonacci<N-2>::value;
};

template<>
struct fibonacci<0>{
static const int value = 0;
};

template<>
struct fibonacci<1>{
static const int value = 1;
};

#define FIB_SEQ1(N) \
BOOST_PP_IF(N,B OOST_PP_COMMA,B OOST_PP_EMPTY)( )\
fibonacci< N >::value

#define FIB_SEQUENCE(z, N,unused) FIB_SEQ1(N)

#define MAX_FIB 46

int rt_fibonacci(in t n)
{
static int const values[] = {
BOOST_PP_REPEAT ( BOOST_PP_ADD(MA X_FIB,1) ,FIB_SEQUENCE,~ )
};
if( (n < 0) || (n > MAX_FIB)) {
throw(std::out_ of_range(
"fibonacci input out of range"
)
);
}
return values[n];
}

int main()
{
for(int i=0;i <=MAX_FIB;++i ){
std::cout << rt_fibonacci(i) << '\n';
}
}

regards
Andy Little

Feb 17 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

28
13102
by: dleecurt | last post by:
Hello, I have a small problem, I am trying to write a program that will calculate the Fibonacci number series, and I have the code complete with one problem. I used a long in to store the numbers, and when the numbers get too large it maxes out the int and I can't count any higher. I am trying to use extremely large numbers, I would like to use up to 10^50 or so. So my question is how do I do this? I'm just learning the language and I...
0
2339
by: Alex Vinokur | last post by:
An algorithm which computes very long Fibonacci numbers http://groups.google.com/groups?selm=bnni5p%2412i47o%241%40ID-79865.news.uni-berlin.de was used as a performance testsuite to compare speed of the code produced by various compilers. =========================================================== Windows 2000 Professional Ver 5.0 Build 2195 Service Pack 2 Intel(R) Celeron(R) CPU 1.70 GHz GNU time 1.7 (to get the real time used)
5
9710
by: Lance | last post by:
What is the correct STL libraries to implement in order to have a Fibonacci Heap created? I am creating an algorithm which would use a Fibonacci Heap. Thanks, Lance
5
7737
by: Niks | last post by:
Can anybody explain me what is a "Fibonacci search"? even an URL will do. Thanks for reading.
4
4852
by: YS Sze | last post by:
If you know the exact longitude and latitude for a specific location, would anyone think it'd make any sense to find out if this set of location numbers is really part of the Fibonacci series or not? Or, another way to look at this is that: Would anyone of you think it is worth a while to find out if there is any location on earth with the set of longitude and latitude numbers that coincides with the Fibonacci series? As I see it, if...
12
2697
by: CII | last post by:
Hi everybody. I've been reading posts a year old about the fibonacci series right on this newsgroup, and while it's not directly games related, I'll share my own notes as well. On another newsgroup there is an ongoing discussion about the famous fibonacci sequences, and a fine program written by a poster got my attention so just for fun I wrote one myself and put it on the web just about two days ago. It can be found on the site ...
3
14792
by: greek | last post by:
Hi! I hav to generate fibonaaci series using recursion: 0,1,1,2,3,5,8,18,21... whr fibonacci(0)=0 fibonacci(1)=1 fibonacci(n)=fibonacci(n-1)+fibonacci(n-2) ive witten the code but having 2 errors:
3
11975
by: veeru | last post by:
Hi All, Can anyone tell about how to create a FIBONACCI series in VB.Net and C# Thanks in Advance, Veeru
13
3168
by: mac | last post by:
Hi, I'm trying to write a fibonacci recursive function that will return the fibonacci string separated by comma. The problem sounds like this: ------------- Write a recursive function that creates a character string containing the first n Fibonacci numbers - F(n) = F(n - 1) + F(n - 2), F(0) = F(1) = 1 -, separated by comma. n should be given as an argument to the program. The recursive function should only take one parameter, n, and...
0
8520
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9057
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8772
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8780
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7603
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6442
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4312
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2208
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1941
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.