fibinocci series

S

shan

Hi to every body,
I am a begginer in C.can anybody give the complete code for fibinocci
series (0 1 1 2 3 5 8 ...)an input is get from user to calculate how
many numbers to be printed.I am using turbo c++.

thank U in advance
 
C

Christopher Benson-Manica

M

Mark McIntyre

Hi to every body,
I am a begginer in C.can anybody give the complete code for fibinocci
series (0 1 1 2 3 5 8 ...)an input is get from user to calculate how
many numbers to be printed.I am using turbo c++.

google is your friend...

Though you may want to spell fibonacci properly....!
 
W

Walter Roberson

I am a begginer in C.can anybody give the complete code for fibinocci
series (0 1 1 2 3 5 8 ...)an input is get from user to calculate how
many numbers to be printed.I am using turbo c++.

What if the user asks for ten billion? What if the user asks
for so many that the number does not fit within an integer?
What if the user asks for negative 17?
 
J

Jordan Abel

What if the user asks for ten billion? What if the user asks
for so many that the number does not fit within an integer?

Such as, say, ten billion? (on many systems in use today, that won't
even fit in an unsigned long)

and of course the series itself will overflow long before you get to
that many.
 
W

Walter Roberson

and of course the series itself will overflow long before you get to
that many.

Well, since the OP asked for "complete" code, I kind of assumed that
the OP would need the indefinite-precision libraries to go along with
everything else...
 
J

Jordan Abel

Well, since the OP asked for "complete" code, I kind of assumed that
the OP would need the indefinite-precision libraries to go along with
everything else...

indefinite-precision, you say?

doubt it'll work on his "turbo c++", but...

system("dc -e '?sc1sx1sy[lxlyplx+sxsy]sI[lIxlc1-sclc0<L]sLlLx'");

....what? what's everyone looking at?
 
W

Walter Roberson

indefinite-precision, you say?
doubt it'll work on his "turbo c++", but...
system("dc -e '?sc1sx1sy[lxlyplx+sxsy]sI[lIxlc1-sclc0<L]sLlLx'");
...what? what's everyone looking at?

$ dc -e '?sc1sx1sy[lxlyplx+sxsy]sI[lIxlc1-sclc0<L]sLlLx'
Usage: dc [OPTION] [file ...]
--help display this help and exit
--version output version information and exit
$ dc
?sc1sx1sy[lxlyplx+sxsy]sI[lIxlc1-sclc0<L]sLlLx
dc: stack empty
1
dc: register 'c' (0143) is empty
dc: stack empty
1
 
J

Jordan Abel

indefinite-precision, you say?
doubt it'll work on his "turbo c++", but...
system("dc -e '?sc1sx1sy[lxlyplx+sxsy]sI[lIxlc1-sclc0<L]sLlLx'");
...what? what's everyone looking at?

$ dc -e '?sc1sx1sy[lxlyplx+sxsy]sI[lIxlc1-sclc0<L]sLlLx'
Usage: dc [OPTION] [file ...]
--help display this help and exit
--version output version information and exit
$ dc
?sc1sx1sy[lxlyplx+sxsy]sI[lIxlc1-sclc0<L]sLlLx
dc: stack empty
1
dc: register 'c' (0143) is empty
dc: stack empty
1

meh

well, this won't let the user input 10 billion, but...

prompt for your input;
FILE *fp = popen("dc","w");
fprintf(fp,"%dsc1sx1sy[lxlyplx+sxsy]sI[lIxlc1-sclc0<L]sLlLx",n);
pclose(fp);

....your dc has gnu options but no ? or -e? - how about the output of
that "version information?

or maybe not, since now we're firmly into off-topic territory.
 
N

Neil Cerutti

Hi to every body,
I am a begginer in C.can anybody give the complete code for
fibinocci series (0 1 1 2 3 5 8 ...)an input is get from user
to calculate how many numbers to be printed.I am using turbo
c++.

thank U in advance

You're welcome in advance.
 
R

Richard Heathfield

shan said:
Hi to every body,
I am a begginer in C.can anybody give the complete code for fibinocci
series (0 1 1 2 3 5 8 ...)an input is get from user to calculate how
many numbers to be printed.I am using turbo c++.

Assuming you mean Fibonacci, see if you can work out why I posted this code:

unsigned long fib(unsigned long n)
{
return n < 2 ? 1 : fib(n - 1) + fib(n - 2);
}
 
K

Keith Thompson

Ah, but that doesn't meet the requested criteria of printing the first
n elements of the series.

No. Why should it?

The OP asked for complete code; Richard chose (wisely, IMHO) not to
provide it.
 
J

Jordan Abel

shan said:


Assuming you mean Fibonacci, see if you can work out why I posted
this code:

unsigned long fib(unsigned long n)
{
return n < 2 ? 1 : fib(n - 1) + fib(n - 2);
}

I don't know, but it runs in O(2^n) time. It's hardly the most
effective way of calculating even one [which can be done in O(N)],
let alone all from 0..some amount, which can be done in O(1) per
number for a total of O(N)
 
R

Richard Heathfield

Jordan Abel said:
I don't know, but it runs in O(2^n) time.

I did indeed say "you"; I guess I should have been more specific.
I meant "you, shan, the chap or chapette with the ostensible
email address (e-mail address removed)".
 
R

Richard Heathfield

Keith Thompson said:
No. Why should it?

The OP asked for complete code; Richard chose (wisely, IMHO) not to
provide it.

Thanks, Keith. It was indeed a choice. In fact, I started out to write a
complete "solution" containing several - um - object lessons, but then I
decided I was hungry. Or something.

I have, in the past, provided very long complete solutions, and have
discovered that they are simply not appreciated by those for whom they were
written, so I stopped bothering. My best-remembered example actually comes
from comp.programming, in which I spent my entire lunch break working on a
solution to a question, and then knocked off early from work so that I
could finish off the solution and post it reasonably promptly. The
resulting article was around 350 lines long, and contained not only a full
analysis of the problem but a complete solution to it in ISO C. The OP
never replied.
 
K

Keith Thompson

Richard Heathfield said:
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/2005
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)

<OT>
Sorry to quote just the sig, but I noticed you've changed the date on
the dmr quotation (it used to be "29 July 1999"). Do I sense another
anecdote, or is it just a glitch?
</OT>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,170
Messages
2,570,925
Members
47,468
Latest member
Fannie44U3

Latest Threads

Top