All pastes #452389 Raw Edit

Anonymous

public text v1 · immutable
·published 2007-04-22 07:18 UTC
rendered paste body
//Very very poor technique.. there must be a better way..
import java.util.Scanner;
import java.util.Arrays;
public class HipToBeSquare
{
        
        public static boolean isSquared(int x, int y, int[] tried){
              if (inTried(tried, y)) return false;
              if (x - y * y == 0){
                  //for (int i: tried) System.out.print(" " + i);
                  return true;
                }
              int newNumber = (x - y * y);
              addTried(tried, y);
              return isSquared(newNumber, highestSquare(newNumber), tried);
            }
        
        public static boolean inTried(int[]tried, int n){
            for(int i: tried){
                if (i == n) return true;
                if (i == 101) return false;
            }
            return false; //SHOULD NOT REACH THIS!
        }
            
        public static void addTried(int[] tried, int n){
            for(int i = 0; i < 100; i++){
                if (tried[i] == 101) tried[i] = n;
            }
        }
        public static int highestSquare(int x){
            for (int i = 1; i <= 100; i++){
            if (x >= i * i) 
                continue;
            else
                return i - 1;
            }
            return 0;
        }
        public static void main(String args[]){
            Scanner reader = new Scanner(System.in);
            System.out.print("Enter a number: ");
            int number = reader.nextInt();
            System.out.println(highestSquare(number));
            int[] tried = new int[100];
            Arrays.fill(tried, 101);
            System.out.println(isSquared(number, highestSquare(number), tried));
            
        
    }
}