Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Mar 6th, 2013  |  syntax: Java  |  size: 1.01 KB  |  views: 18  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. import java.util.HashMap;
  2.  
  3. public class BytelandianConsistencyCheck {
  4.        
  5.         private static HashMap<Long, Long> memory = new HashMap<Long, Long>();
  6.         private static HashMap<Long, Long> map = new HashMap<Long, Long>();
  7.         public static final long LOWER_BOUND = 10_000_000_000L;
  8.         public static final long UPPER_BOUND = 10_000_500_000L;
  9.        
  10.         public static void main(String[] args) {
  11.                 memory.put(0L, 0L);
  12.                
  13.                 for (long i = LOWER_BOUND; i < UPPER_BOUND; i++){
  14.                         if (coins(i) != changer(i)){
  15.                                 System.out.println("The methods didn't return the same value");
  16.                         }
  17.                 }
  18.         }
  19.  
  20.  
  21.         public static Long changer(long coin) {
  22.                 if (!memory.containsKey(coin)){
  23.                         long tmp = changer(coin/4L) + changer(coin/3L) + changer(coin/2L);
  24.                         memory.put(coin, tmp > coin ? tmp : coin);
  25.                 }
  26.                
  27.                 return memory.get(coin);
  28.         }
  29.        
  30.         public static long coins(long n) {
  31.                 if (!map.containsKey(n)) {
  32.                         map.put(n, (n > (n / 2) + (n / 3) + (n / 4) ? n : coins(n / 2)
  33.                                         + coins(n / 3) + coins(n / 4)));
  34.                 }
  35.                 return map.get(n);
  36.         }
  37.  
  38. }
clone this paste RAW Paste Data