Skip to content

Instantly share code, notes, and snippets.

@RupGautam
Created February 24, 2017 01:27
Show Gist options
  • Select an option

  • Save RupGautam/b3c79bbaa534589eb589b8776c41178d to your computer and use it in GitHub Desktop.

Select an option

Save RupGautam/b3c79bbaa534589eb589b8776c41178d to your computer and use it in GitHub Desktop.
/**
* Created by RupGautam on 2/22/17.
*/
import java.util.Random;
import java.util.Scanner;
/**
* Copyright ${COPYRIGHT}.
*/
public class test {
public static void main(String[] args) {
int[] a = new int[1000];
int old_n = 0, n = 0;
int step = 0;
a[n++] = 25;
int goal = 11;
boolean found = a[0] == goal;
while (!found) {
step++;
System.out.print("" + step + " steps: ");
int start_n = old_n;
old_n = n;
for (int i = start_n; i < old_n; i++)
for (int opt = 0; opt < 2; opt++) {
int new_a = -1;
switch (opt) {
case 0:
if (a[i] % 2 == 0)
new_a = a[i] / 2;
break;
case 1:
new_a = a[i] + 3;
}
if (new_a < 0)
continue;
if (new_a == goal)
found = true;
int j = 0;
while (j < n && a[j] != new_a)
j++;
if (j >= n) {
System.out.print(" " + new_a);
a[n++] = new_a;
}
}
System.out.println();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment