Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package me.evyatar.first;
- import java.util.Arrays;
- import java.util.HashMap;
- import java.util.Map.Entry;
- public class Ackermann {
- public static HashMap<String, Integer> repetition = new HashMap<>(),
- values = new HashMap<>();
- public static int total = 0;
- public static int Ackermann(int x, int y) {
- String str = String.format("(%s, %s)", x, y);
- Integer i;
- if ( ( i = repetition.get((str))
- ) == null) {
- repetition.put(str, 1);
- }
- else {
- repetition.put(str, i+1);
- return values.get(str);
- }
- total++;
- if (x == 0)
- values.put(str, i = y+1);
- else if (y == 0)
- values.put(str, i = Ackermann(x-1, 1));
- else
- values.put(str, i = Ackermann(x-1, Ackermann(x, y-1)));
- return i;
- }
- public static void main(String[] args) {
- System.out.println(Arrays.asList(args));
- System.out.println(String.format("(%s, %s)", 1, 2));
- System.out.println(Ackermann(3, 8));
- System.out.println("Statistics: ");
- for (Entry<String, Integer> e : repetition.entrySet()) {
- System.out.println(String.format("%s used %s times", e.getKey(), e.getValue()));
- }
- System.out.println("Total computations of " + total);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement