Advertisement
Evyatar12

Untitled

Aug 17th, 2017
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.22 KB | None | 0 0
  1. package me.evyatar.first;
  2.  
  3. import java.util.Arrays;
  4. import java.util.HashMap;
  5. import java.util.Map.Entry;
  6.  
  7. public class Ackermann {
  8.    
  9.     public static HashMap<String, Integer> repetition = new HashMap<>(),
  10.                                            values = new HashMap<>();
  11.     public static int total = 0;
  12.    
  13.     public static int Ackermann(int x, int y) {
  14.         String str = String.format("(%s, %s)", x, y);
  15.         Integer i;
  16.        
  17.        
  18.        
  19.         if ( ( i = repetition.get((str))
  20.                 ) == null) {
  21.             repetition.put(str, 1);
  22.         }
  23.         else {
  24.             repetition.put(str, i+1);
  25.             return values.get(str);
  26.         }
  27.        
  28.         total++;
  29.        
  30.         if (x == 0)
  31.             values.put(str, i = y+1);
  32.         else if (y == 0)
  33.             values.put(str, i = Ackermann(x-1, 1));
  34.         else
  35.             values.put(str, i = Ackermann(x-1, Ackermann(x, y-1)));
  36.        
  37.         return i;
  38.     }
  39.    
  40.     public static void main(String[] args) {
  41.         System.out.println(Arrays.asList(args));
  42.        
  43.         System.out.println(String.format("(%s, %s)", 1, 2));
  44.         System.out.println(Ackermann(3, 8));
  45.        
  46.        
  47.         System.out.println("Statistics: ");
  48.        
  49.         for (Entry<String, Integer> e : repetition.entrySet()) {
  50.             System.out.println(String.format("%s used %s times", e.getKey(), e.getValue()));
  51.         }
  52.        
  53.         System.out.println("Total computations of " + total);
  54.     }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement