0

I don't know what I'm doing wrong. I'm trying to print "fn" of the sequence. I based it on someone else's code. Any thoughts?

Here is my code:

int num = n;
int fn = 1;
int f1 = 0;
int f2 = 1;

for (int i = 2; i < n; i++)
{
 fn = f1 + f2;
 f1 = f2;
 f2 = fn;

}

System.out.print( "fib( " + num + " ) is ");
System.out.println( fn );
  • 4
    What's the problem? Does it not print what you want? Does it not compile? – cyon Sep 27 '13 at 16:27
  • `n` is never initialized as far as I can tell. – nhgrif Sep 27 '13 at 16:29
  • What @cyon said, plus: What's "`n`"? Why are you using both "`n`" and "`num`"? What do you expect `fib(0)` to return? – CPerkins Sep 27 '13 at 16:31
  • possible duplicate of [Determining whether a number is a Fibonacci number](http://stackoverflow.com/questions/3139645/determining-whether-a-number-is-a-fibonacci-number) – fvrghl Sep 27 '13 at 16:31
  • This should work fine for all `n`s between 2 and 46, inclusive. After 46 you will see an `int` overflow. – Sergey Kalinichenko Sep 27 '13 at 16:32
  • In this fragment, yes. I assumed `n` was set elsewhere... if you add a line doing so (e.g. `n = 10`) then the code works fine. For `n=10` it prints out `fib(10) = 34`... so it's off by one since generally the sequence starts with `F(0) = 0`, `F(1) = 1` and works up to `F(10) = 55`. – dcsohl Sep 27 '13 at 16:32
  • Well, apart from not catering for `fib(0)`, I ran your program and the output looks right (except for a possible index-of-by-one error, as there are two accepted definitions of where you should start). You really need to tell us what you think is wrong. – Bernhard Barker Sep 27 '13 at 16:33

4 Answers4

2

I ran your code to see if there is a problem. Here are various program outputs for values 2-10:

fib( 2 ) is 1
fib( 3 ) is 1
fib( 4 ) is 2
fib( 5 ) is 3
fib( 6 ) is 5
fib( 7 ) is 8
fib( 8 ) is 13
fib( 9 ) is 21

The output is close to being correct, except you are getting the result for fib(n - 1) each time. The reason is that you are stopping your for loop one iteration early. Try modifying your condition with <=:

for (int i = 2; i <= n; i++)

Output:

fib( 2 ) is 1
fib( 3 ) is 2
fib( 4 ) is 3
fib( 5 ) is 5
fib( 6 ) is 8
fib( 7 ) is 13
fib( 8 ) is 21
fib( 9 ) is 34
rgettman
  • 176,041
  • 30
  • 275
  • 357
1
static int fib(int n) {
    if (n == 0 || n == 1)
        return n;

    int f1 = 0;
    int f2 = 1;
    int fn = 0;

    for (int i = 2; i <= n; i++) {
        fn = f2 + f1;
        f1 = f2;
        f2 = fn;
    }

    return fn;
}
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
1

A Fibonacci sequence is calculated by adding the previous two members of the sequence:

0 1         --the series starts like this.
0+1=1       so the series is now 
0 1 1       
  1+1=2     so the series continues...
0 1 1 2     and the next term is
    1+2=3   so we now have
0 1 1 2 3   and it continues as follows ...

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, ...

Are you considering the case 0?

e.g.:

int n = Integer.parseInt(JOptionPane.showInputDialog("Input number:"));
BigInteger a = BigInteger.ONE;
BigInteger b = BigInteger.ZERO;
BigInteger c;
for (int i = 1; i <= n; i++) {
     c = a.add(b);
     a = b;
     b = c;
}
System.out.println(b);

f(12345) =

40080569507224047097051499321406575219228944077206339223411612103596633062182105
01082846030337166327710866380461665776652058343623273978850095367908925248215121
45173749742393351263429067658996935575930135482780507243981402150702461932551227
59043371327725570529753742801795702653627925205323772902863350712348310321084661
77747639361546735226645917360810397092944238656680469254927475839537583258506135
48914282578320544573036249175099094644435323970587790740267131607004023987409385
71616246095570779325753211277193270481671351919612883447072183609426501291804642
74491566540671950713589551040979737101509205368478774342567798867295556912132825
04703193401739340461924048504866698176130757935914248753973087073009601101912877
38363462892946760898398066418536337028673177171254258304136532864812454932387880
67583956523408611863340273923070910792571808356729897985240845346772523695859184
58720952520972332496025465803523315515681084895362126005441170936820059518262349
02245688875893867292085573973642391706512281634319217227130198100763607075137844
13630911872895221442278513821978071942563922949199120370194765824184512737679767
83751999133072126657949249799858935787018952232743400610036315564885371356712960
60896675518661262042586889262110662782513742538683165736882639824560614794427399
84983564433621701332349245316739393036680428782582821042127696252456803213440344
42698232414181912301904509531018692483863038992377680591406376081935756597411807
86483245242199312145954905504225330554559400911075373030206188102518205307407793
04945743042843818905340530656390842536418813634633111840242818352651038845390128
74542416238100890688593076189105555658375552988619203325356676814545718066196038
34568467183010292020985768291297156583889601129491834908879218410831868929923078
83556186380401867907243510736502105144291149055354110448887747138600413415933183
65792673354888566799196442017231870631867558530906286613228902689695061557951752
30968780656757329091090953539575814899437715863705011234765151784718812379079423
15727293456176196775555832070122531017013289717688278619224080643798912019728815
54890367344239218306050355964382953279316318309272212482218232309006973312977359
56255318460814457171307380228567550320922958131205725972936238278618310034396148
40908660575604740441898706339122005954780515737698899683422035125503026551174917
40823696686983281784153050366346823513213598551985596176977626982962058849363351
79430220670390757797006579383951159193074144107923417994348020653976756124427132
59233437520710389680021578899126949472040036377912710841909290583698015317878874
44598295425899927970
Paul Vargas
  • 41,222
  • 15
  • 102
  • 148
1

You can also try the recursive algorithm...

    if(n == 0)
        return 0;

    else if(n == 1)
        return 1;

    else
        return ( fibonacci(n - 1) + fibonacci(n - 2) );