Pages

Showing posts with label combinations. Show all posts
Showing posts with label combinations. Show all posts

Tuesday, September 8, 2009

Euler Problem 31 solution

Time (s): ~0.001
package margusmartseppcode.From_30_to_39;

public class Problem_31 {
 public static void main(String[] args) {
  int target = 200, coins[] = { 1, 2, 5, 10, 20, 50, 100, 200 };
  int ways[] = new int[target + 1];
  ways[0] = 1;

  for (int coin : coins)
   for (int i = coin; i < target + 1; i++)
    ways[i] += ways[i - coin];

  System.out.println(ways[target]);
 }
}

Euler Problem 15 solution

Time (s): ~0.001
package margusmartseppcode.From_10_to_19;

import java.math.BigInteger;

public class Problem_15 {
 static final int maxSize = 42;
 static BigInteger f[] = new BigInteger[maxSize];
 static {
  f[0] = BigInteger.ONE;
  for (int i = 1; i < maxSize; i++)
   f[i] = f[i - 1].multiply(BigInteger.valueOf(i));
 }

 public static void main(String[] args) {
  // s=x=y
  // !(x+y)/!x*!y
  int s = 20;
  System.out.println(f[s + s].divide((f[s].multiply(f[s]))));
 }
}