*
A friendly place for programming greenhorns!
Big Moose Saloon
Search

Java FAQ

Recent Topics

Flagged Topics

Hot Topics

Zero Replies
Register / Login
JavaRanch
»
Java Forums
»
Java
»
Java in General
Author
BigInteger: strange problem
Randall Twede
Ranch Hand
Joined: Oct 21, 2000
Posts: 4339
2
I like...
posted
Jul 30, 2012 13:03:33
0
i finally found a formula for solving euler116. it worked fine using longs and a smaller input but i had to change to
BigInteger
. the result is strange. i have these lines of code
BigInteger
count = BigInteger.ZERO;
BigInteger
quotient = num.divide(den);
System.out.println(quotient);
count.add(quotient);
System.out.println(count);
it prints out (for example)
9
0
how is this possible???
if quotient is 9 and count starts out being 0 it should print out 9!!!
i can post entire code if necessary
SCJP
Visit my
download page
Randall Twede
Ranch Hand
Joined: Oct 21, 2000
Posts: 4339
2
I like...
posted
Jul 30, 2012 13:16:24
0
here is entire code
import java.math.BigInteger; class Euler119 implements Problem { final int spaces = 10; BigInteger count = BigInteger.ZERO; public String solve() { ways(2); BigInteger answer = count; //ways(3); //answer.add(BigInteger.valueOf(count)); //ways(3); //answer.add(BigInteger.valueOf(count)); return answer.toString(); } //b = 50(L*c) //(c+b)! / c!b! private void ways(int size) { for( int tiles = 1; tiles <= spaces / size; tiles++) { BigInteger num = factorial(tiles + (spaces  size * tiles)); System.out.println(num); BigInteger den = factorial(tiles).multiply(factorial(spaces  size * tiles)); System.out.println(den); BigInteger quotient = num.divide(den); System.out.println(quotient); count.add(quotient); System.out.println(count); } } private BigInteger factorial(long number) { BigInteger answer = BigInteger.ONE; for(long i = 1; i <= number; i++) { BigInteger next = BigInteger.valueOf(i); answer = answer.multiply(next); } return answer; } public String getMessage() { return "test"; } }
it prints out
362880
40320
9
0
40320
1440
28
0
5040
144
35
0
720
48
15
0
120
120
1
0
Randall Twede
Ranch Hand
Joined: Oct 21, 2000
Posts: 4339
2
I like...
posted
Jul 30, 2012 13:24:49
1
never mind. this is the second time i made this mistake.
should be count = count.add(quotient);
I agree. Here's the link:
http://aspose.com/filetools
subject: BigInteger: strange problem
Similar Threads
Question About Nested Loops
prime numbers
Recursion Problem
BigInteger Operations
Base Conversions
All times are in JavaRanch time: GMT6 in summer, GMT7 in winter