Skip to content

Commit

Permalink
P24, P25, P31: Tweaked solution code in simple ways without changing …
Browse files Browse the repository at this point in the history
…behavior.
  • Loading branch information
nayuki committed Jan 23, 2017
1 parent 92cd80b commit d88cbc4
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 11 deletions.
2 changes: 1 addition & 1 deletion haskell/p025.hs
Expand Up @@ -16,7 +16,7 @@

digits = 1000
main = putStrLn (show ans)
ans = length (takeWhile (< 10 ^ (digits - 1)) fibonacci)
ans = length (takeWhile (< 10^(digits-1)) fibonacci)

fibonacci :: [Integer]
fibonacci = 0 : 1 : (zipWith (+) fibonacci (tail fibonacci))
12 changes: 5 additions & 7 deletions java/p025.java
Expand Up @@ -26,22 +26,20 @@ public static void main(String[] args) {
private static final int DIGITS = 1000;

public String run() {
BigInteger lowerthres = BigInteger.TEN.pow(DIGITS - 1);
BigInteger upperthres = BigInteger.TEN.pow(DIGITS);
BigInteger lowerThres = BigInteger.TEN.pow(DIGITS - 1);
BigInteger upperThres = BigInteger.TEN.pow(DIGITS);
BigInteger prev = BigInteger.ONE;
BigInteger cur = BigInteger.ZERO;
int i = 0;
while (true) {
for (int i = 0; ; i++) {
// At this point, prev = fibonacci(i - 1) and cur = fibonacci(i)
if (cur.compareTo(lowerthres) >= 0)
if (cur.compareTo(lowerThres) >= 0)
return Integer.toString(i);
else if (cur.compareTo(upperthres) >= 0)
else if (cur.compareTo(upperThres) >= 0)
throw new RuntimeException("Not found");

BigInteger temp = cur.add(prev);
prev = cur;
cur = temp;
i++;
}
}

Expand Down
3 changes: 2 additions & 1 deletion java/p031.java
Expand Up @@ -28,8 +28,9 @@ public String run() {
int[][] ways = new int[COINS.length + 1][TOTAL + 1];
ways[0][0] = 1;
for (int i = 0; i < COINS.length; i++) {
int coin = COINS[i];
for (int j = 0; j <= TOTAL; j++)
ways[i + 1][j] = ways[i][j] + (j >= COINS[i] ? ways[i + 1][j - COINS[i]] : 0); // Dynamic programming
ways[i + 1][j] = ways[i][j] + (j >= coin ? ways[i + 1][j - coin] : 0); // Dynamic programming
}
return Integer.toString(ways[COINS.length][TOTAL]);
}
Expand Down
2 changes: 0 additions & 2 deletions python/p024.py
Expand Up @@ -7,8 +7,6 @@
#

import itertools, sys
if sys.version_info.major == 2:
range = xrange


# We initialize a list as the lowest permutation of the given digits, which is the sequence
Expand Down

0 comments on commit d88cbc4

Please sign in to comment.