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 14 4769
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 fe**********@hotmail.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 fe**********@hotmail.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
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.
In article <11**********************@f14g2000cwb.googlegroups .com>, fe**********@hotmail.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. fe**********@hotmail.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
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
* Daniel T.: * fe**********@hotmail.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?
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.programming. Here it is:
/*
run time fibonacci
*/
//Boost.Preprocessor 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,BOOST_PP_COMMA,BOOST_PP_EMPTY)()\
fibonacci< N >::value
#define FIB_SEQUENCE(z,N,unused) FIB_SEQ1(N)
#define MAX_FIB 46
int rt_fibonacci(int n)
{
static int const values[] = {
BOOST_PP_REPEAT( BOOST_PP_ADD(MAX_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 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;
Is it not the divsor that controls the return type of operator/ ?
As in a/double(b)?
* je****@alphacash.se: 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; Is it not the divsor that controls the return type of operator/ ? As in a/double(b)?
No, the conversion does not depend on the particular operator involved.
With two arguments of different types, the type that has the largest set
of values (imprecisely speaking) determines the type of the expression,
and the other argument is converted "up" to that type.
IIRC this falls under the "usual arithmetic conversions" in the standard.
--
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?
On 2006-02-17 07:22:28 -0500, an**@servocomm.freeserve.co.uk said:
>
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.programming. Here it is:
/*
run time fibonacci
*/
//Boost.Preprocessor 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,BOOST_PP_COMMA,BOOST_PP_EMPTY)()\
fibonacci< N >::value
#define FIB_SEQUENCE(z,N,unused) FIB_SEQ1(N)
#define MAX_FIB 46
int rt_fibonacci(int n)
{
static int const values[] = {
BOOST_PP_REPEAT( BOOST_PP_ADD(MAX_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
Of course, you can directly compute any particular fibonacci number
directly by using the Binet formula. No need to jump through all the
hoops of calculating x-1 and x-2 and certainly no need for recursion.
Bonus: It takes only 1 line of c++ code.
On 3 Okt, 04:53, Mark Casternoff <m...@foobar.netwrote:
On 2006-02-17 07:22:28 -0500, a...@servocomm.freeserve.co.uk said:
FWIW I justput up an O(1) solution on comp.programming. Here it is:
/*
* * run time fibonacci
*/
//Boost.Preprocessor library
// available athttp://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,BOOST_PP_COMMA,BOOST_PP_EMPTY)()\
fibonacci< N >::value
#define FIB_SEQUENCE(z,N,unused) FIB_SEQ1(N)
#define MAX_FIB 46
int rt_fibonacci(int n)
{
* * static int const values[] *= {
* * * *BOOST_PP_REPEAT( BOOST_PP_ADD(MAX_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
Of course, you can directly compute any particular fibonacci number
directly by using the Binet formula. *No need to jump through all the
hoops of calculating x-1 and x-2 and certainly no need for recursion. *
Bonus: *It takes only 1 line of c++ code.
Andy's solution would still outperform it (in runtime) as it is just a
table lookup. Unless, of course, you implement the Binet formula using
template metaprogramming. I'd really like to see that done in one
line :)
On Oct 2 2008, 10:53*pm, Mark Casternoff <m...@foobar.netwrote:
On 2006-02-17 07:22:28 -0500, a...@servocomm.freeserve.co.uk said:
Daniel T. wrote:
[snip]
Wow. You must have a *really* slow internet connection.
Socks This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
by: Niks |
last post by:
Can anybody explain me what is a "Fibonacci search"?
even an URL will do.
Thanks for reading.
|
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...
|
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,...
|
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...
|
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
|
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:...
|
by: tammygombez |
last post by:
Hey fellow JavaFX developers,
I'm currently working on a project that involves using a ComboBox in JavaFX, and I've run into a bit of an issue....
|
by: concettolabs |
last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
|
by: better678 |
last post by:
Question:
Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct?
Answer:
Java is an object-oriented...
|
by: teenabhardwaj |
last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
|
by: CD Tom |
last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
|
by: Naresh1 |
last post by:
What is WebLogic Admin Training?
WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
|
by: antdb |
last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine
In the overall architecture, a new "hyper-convergence" concept was...
|
by: Matthew3360 |
last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function.
Here is my code.
...
|
by: Matthew3360 |
last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
| |