• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Can java handle numbers greater than one billion?

 
Ranch Hand
Posts: 85
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ranchers, i'm having fibonacci numbers in an array of type int[]. The numbers are up to 4million. Since I generated it with a loop, i have no idea of what the 3999998 element will be. On invoking println, i knew the answer was wrong because it printed a negative number. But i'm very sure it works for like the 1st 1000 elements or very much more. Thanks in advance!
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ofcourse Java can handle numbers greater than one billion. But an int is a 32-bit number, which can store values between -2^32 and 2^32 - 1, so you won't be able to store numbers larger than 2^32 - 1 = 4,294,967,295 in an int.

You could use a long which has 64 bits, but the Fibonacci sequence grows so fast that you'll quickly run out of the range of the long type too.

To store integer numbers with an arbitrary number of digits, you can use class BigInteger.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Shamsudeen Akanbi wrote:Hi ranchers, i'm having fibonacci numbers in an array of type int[]. The numbers are up to 4million. Since I generated it with a loop, i have no idea of what the 3999998 element will be. On invoking println, i knew the answer was wrong because it printed a negative number. But i'm very sure it works for like the 1st 1000 elements or very much more. Thanks in advance!


This sounds like one of those Euler Project problems.

A nice property to remember about the Fibonacci series is that F(n)/F(n-1) approaches the Golden Ratio: (√5+1)/2 ≈ 1.618...

Therefore, results are going to progress in powers of 1.618, which is larger than √2 (1.414...), and that means that they are going to more than double for every other result. So, in order to get F(3999998) you need a number that holds at least 2 million bits (and probably a lot more).

Luckily, there is a class that can hold such values easily: BigInteger.

What may not be so easy is waiting for the result of F(3999998)...but good luck in your quest.

Winston

[Edit] Actually, the "therefore" above may be wrong; but the rest of what I said is correct.
 
Bartender
Posts: 1464
32
Netbeans IDE C++ Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nice analysis, Winston! I love to see people apply mathematics to computation. Not enough of that in our field, imho.

(And I think your use of "therefore" is okay, fwiw.)
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stevens Miller wrote:Nice analysis, Winston! I love to see people apply mathematics to computation. Not enough of that in our field, imho.


Cheers mate...although in my case it's more 'geek' than Maths (only got to A-level). I still like it though (when I can follow it ).

And I think your use of "therefore" is okay, fwiw...


And that's where my Maths fails. I believe (but I may be wrong here) that Fibonacci produces 'progressive closest integral fractions to G' and, if that's right, then the rest would be right by inference; but if not, I think it would be a lot more difficult to prove the "therefore" bit.

But I'd love to know if I'm wrong...and therefore right.

Winston
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:I believe (but I may be wrong here) that Fibonacci produces 'progressive closest integral fractions to G'...


Actually, I think I may have worked it out for myself: If F(n) == F(n-1) + F(n-2), then it doesn't matter if progressive results of F(n)/F(n-1) produce the closest fraction to G or not; simply that they produce a closer one - and that I'm pretty certain they do.

Mathematicians, please put me out of my ignorance. My tools are teenage Maths and logic.

Winston

 
Stevens Miller
Bartender
Posts: 1464
32
Netbeans IDE C++ Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's a proof that meets your needs, I think.
 
Shamsudeen Akanbi
Ranch Hand
Posts: 85
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got an error that: BigInteger(long) has private access in BigInteger. BigInteger total=new BigInteger(0);. And an arrow was pointed at 'n' in new.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stevens Miller wrote:Here's a proof that meets your needs, I think.


Yup. That'd be it. Cheers. Logic had basically sorted it for me; but sometimes you like the Linus blanket of a mathematical proof.

Winston
 
Stevens Miller
Bartender
Posts: 1464
32
Netbeans IDE C++ Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Shamsudeen Akanbi wrote:I got an error that: BigInteger(long) has private access in BigInteger. BigInteger total=new BigInteger(0);. And an arrow was pointed at 'n' in new.



Shamsudeen, have a look at the list of constructors available when you use the BigInteger class. BigInteger(long) isn't one of them. BigInteger("0") might get you going, but read the documentation before you assume that will work for you.

Hey! This is my 30'th post! I'm a Ranch Hand!
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stevens Miller wrote:Hey! This is my 30'th post! I'm a Ranch Hand!


Stevens, let me be the first, mate....

Congratulations.

Now go poke a few more cows.

Winston
 
Stevens Miller
Bartender
Posts: 1464
32
Netbeans IDE C++ Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:

Stevens Miller wrote:Hey! This is my 30'th post! I'm a Ranch Hand!


Stevens, let me be the first, mate....

Congratulations.



Why, thank you, Winston!

We don't say "mate" much here on the American Atlantic Coast (well, not as a noun, anyway). Where are you located?
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stevens Miller wrote:We don't say "mate" much here on the American Atlantic Coast (well, not as a noun, anyway). Where are you located?


Currently: Brussels (for the last 8 years); but originally a Canuck, brought up on the 'Sarf' coast of England.

Before 'La belle Belgique', had split my life pretty much equally between the UK and Canada - with 18 months in the US (NYC, IL and MN) back in '79-80 as my re-intro to 'Norf' America.

So you'll forgive any confusion (comes out in my posts sometimes) when it comes to "nationhood". Personally, I see myself as a "Brit with other bits".

Winston
 
Stevens Miller
Bartender
Posts: 1464
32
Netbeans IDE C++ Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cool! How's the market for programmers out there? Need any help?
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stevens Miller wrote:Cool! How's the market for programmers out there? Need any help?


What, me personally, or my "nation" (UK, I guess) as a whole?

In general, I'd say that the economy over here isn't quite as healthy as yours; but there are jobs out there for people with the "right" skills.
What's "right"? Dunno. These days, it seems to involve a lot of TLA's on your CV; and it also helps if you ain't 55 (like me) and perceived as a dinosaur.

I'll be a programmer until they curl me up and chuck me in the furnace, job or no job; but if you're looking for pastures new, have a look at the ads - you guys can get the Times or the FT same day can't you?

Britain's a wonderful country; but be prepared for some changes if you do decide to make the hop. It's more prosaic, more wordy, and much, much smaller. And it's GREEN. Living on the East Coast you may be used to it, but I lived in Vancouver, BC for most of my 22 years in Canada; just south of 300-odd thousand square miles of pine forest. And believe me, you notice it when you're on the glide path into Heathrow.

Winston
 
Stevens Miller
Bartender
Posts: 1464
32
Netbeans IDE C++ Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I turn 54 in October, so I guess we can look down our withered old noses at these newcomers together.

I live in the glide-path for Dulles International, so I can relate.

Say, back to Java for a moment: I'm having a heck of a time with an interface I added to a .jar file that is already working well as a source of classes for a different project. I added an interface to the .jar, and now my main project can't find the interface when I compile the main project. Any pointers for me to some guidance?
 
Stevens Miller
Bartender
Posts: 1464
32
Netbeans IDE C++ Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, solved my own problem, sort of... The "bug" was actually a problem with how I had the NetBeans build.xml executing javah. If I had known I was seeing output from javah, I'd have fixed it right away.

Accordingly, I've created a new topic to ask how to get informational message out of build.xml. If anyone knows, please say so at https://coderanch.com/t/589286/java/java/Make-build-xml-File-Print#2683450
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stevens Miller wrote:Well, solved my own problem, sort of...


Yeah, sorry about that. You caught me Olympics-ing.

Glad you got it sorted anyway.

Winston
 
Look! I laid an egg! Why does it smell like that? Tiny ad, does this smell weird to you?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic