Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-12-2008, 10:44 PM
Member
 
Join Date: Dec 2008
Posts: 5
Rep Power: 0
thekrazykid is on a distinguished road
Default help with fibonacci problem
ok so i want to find the position in the fibonacci series of the first number with 1000 digits.. heres what i have so far
Code:
public class pe25 {
    public static void main(String [] args) {
        //initialize variables
        Integer a = new Integer(0);
        Integer b = new Integer(1);
        String s = ""; // would just put String s; but i need to use the length
                       // of it in the while loop
        int count = 0; // what number in the series the number is
        while (s.length() < 1000){
            count++;
            b = a + b;
            a = b - a;
            s = a.toString();
            System.out.println(s);// just for testing
        }
        System.out.println(count);
    }

}
however heres the first numbers in the output..
Code:
1
1
2
3
5
8
13
21
34
55
89
144
233
377
610
987
1597
2584
4181
6765
10946
17711
28657
46368
75025
121393
196418
317811
514229
832040
1346269
2178309
3524578
5702887
9227465
14930352
24157817
39088169
63245986
102334155
165580141
267914296
433494437
701408733
1134903170
1836311903
-1323752223
512559680
-811192543
-298632863
-1109825406
-1408458269
1776683621
368225352
2144908973
-1781832971
notice how they are right for a while then suddenly jump to being negative..
i have no idea why this isnt working as i programming basically the exact same thing in python and it works perfectly.. heres the python code if anyone cares..
Code:
a = 0
b = 1
count = 0
while len(str(a)) < 1000:
    count += 1
    b = a+b
    a = b-a
print count
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 12-12-2008, 11:11 PM
xcallmejudasx's Avatar
Senior Member
 
Join Date: Oct 2008
Location: Houston, TX & Flint, MI
Posts: 585
Rep Power: 2
xcallmejudasx is on a distinguished road
Send a message via AIM to xcallmejudasx
Default
Your number is becoming too large for an Integer to hold. Try using long or BigInteger.

Your code breaks at right under 3 billion or about 2^32. I'm not sure the highest that Integer can hold(perhaps someone can toss that in?)
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 12-12-2008, 11:24 PM
Member
 
Join Date: Dec 2008
Posts: 5
Rep Power: 0
thekrazykid is on a distinguished road
Default
ahh i see thanks.. heres what i ended up with
Code:
import java.math.BigInteger;

public class pe25 {
    public static void main(String [] args) {
        //initialize variables
        BigInteger a = new BigInteger("0");
        BigInteger b = new BigInteger("1");
        String s = ""; // would just put String s; but i need to use the length
                       // of it in the while loop
        int count = 0; // what number in the series the number is
        while (s.length() < 1000){
            count++;
            b = a.add(b);
            a = b.subtract(a);
            s = a.toString();
        }
        System.out.println(count);
    }

}
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 12-12-2008, 11:38 PM
Senior Member
 
Join Date: Nov 2007
Posts: 155
Rep Power: 3
carderne is on a distinguished road
Default
To see the highest value of an Integer:
System.out.println(Integer.MAX_VALUE);
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 12-12-2008, 11:41 PM
xcallmejudasx's Avatar
Senior Member
 
Join Date: Oct 2008
Location: Houston, TX & Flint, MI
Posts: 585
Rep Power: 2
xcallmejudasx is on a distinguished road
Send a message via AIM to xcallmejudasx
Default
I'm assuming BigInteger worked then?

And thanks carderne I always forget I can view public variables within java classes(even though I always use MyClass.myVariable lol)
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Printing Fibonacci Numbers Java Tip java.lang 0 04-09-2008 07:43 PM
A Fibonacci printing program Java Tip Java Tips 0 03-28-2008 08:26 PM
Computing Fibonacci numbers recursively Java Tip Java Tips 0 01-22-2008 09:20 PM
Fibonacci Algorithm susan New To Java 1 08-07-2007 05:25 AM


All times are GMT +2. The time now is 10:14 PM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org