Question
A Happy Number is a number defined by the following process: Starting with any positive integer, replace the number by the squares of each of
A Happy Number is a number defined by the following process: Starting with any positive integer, replace the number by the squares of each of the digits within the number. Repeat the process until the sum of the digits is equal to one (the number is happy) or the number repeats itself (the number is not happy).
[From: https://en.wikipedia.org/wiki/Happy_number#Happy_primes]
An example of a valid Happy Number is 487.
First compute the sum of the squares of the digits: 42 + 82 + 72 = 16 + 64 + 49 = 129.
Then compute the sum of the digits: 1 + 2 + 9 = 12.
If the sum of the digits is 1, return true. However, its not, so continue.
Check to see if sum of the squares, 129, is in the repeat list. If so, return false we detected a repeating pattern.
Add the number 129 to the repeat list.
Continue the process with the number 129.
Compute the sum of the squares of the digits: 12 + 22 + 92= 86.
Compute the sum of the digits: 8 + 6 = 14
Sum is not 1, continue.
Check to see if 86 is in the repeat list. Its not.
Add the number 86 in the repeat list. List is now 487, 129, and 86.
Continue with the number 86.
Compute the sum of the squares of the digits: 82 + 62= 100.
Compute the sum of the digits: 1
Sum is 1, return true. The number 487 is a happy number.
See the referenced web site above for additional examples.
In this lab, we will write a recursive method to determine if a given number is happy and also prime. You will create a HappyNumber class which will have an ArrayList
The class should have the following methods:
public boolean isHappy( int number )
public boolean isPrime (int number)
public boolean isHappyPrime (int number)
The method isHappy() needs to be recursive. isPrime() does not need to be recursive and isHappyPrime() will call both isHappy() and isPrime().
You will write a test application that will display all the numbers below 1000 that are both Prime and Happy.
EightQueens.java
/* * EightQueens: Can you fit 8 queens on a chess board so that they can not "take" * each other? * This program shows a recursive solution to finding the solution. * It starts off selecting a row for column 0 and then finding the * corresponding solution. * * To complete this lab, you'll need to complete the following 2 * methods: * - validateHorizontal(int row) * - validateDiagonal(int row, int col) * * Please try to dive into how this program works so you can appreciate * a recursive solution. Could you write an iterative solution? */ public class EightQueens { static final int MAX_ROWS = 8; static final int MAX_COLMS = MAX_ROWS; /* * queens array: there can only be 1 queen per row and column, so we do not * need a 2 dimensional array. The index is the column and * the element is the row#. If the element is -1, we have not * placed a queen in that column. * * example: queens = {4, 0, 3, 5, -1, -1, -1, -1} * => we currently have 4 queens on the board at locations: * (4, 0), (0, 1), (3, 2), and (5, 3) */ static int [] queens = new int [MAX_ROWS]; static int numIterations=0; public static void printQueenArray(int row) { for (int k=0; k<=row; k++) System.out.print(queens[k] + " "); System.out.println(); } /* * validateHorizontal: verify there is not a queen in the specified row. */ public static boolean validateHorizontal(int row) { //TODO: return true; } /* * validateDiagonal: verify thete is not a queen in the diagonal direction (45 degrees) * - you only need to look backwards from the specified column * - look in 2 directions: * negative slope: row-1, col-1 * positive slope: row+1, col-1 * - return false if there is a conflict with another queen. * - return true if there is no conflict. */ public static boolean validateDiagonal(int row, int col) { //verify going up (negative slope) //verify going down (positive slope) return true; } /* * tryQueen: recursive method to find a solution of where to * fit a Queen from c to MAX_COLMS */ public static boolean tryQueen(int c) { numIterations++; //base condition: done when we have tried all columns if (c >= MAX_COLMS) return true; //find a valid row for the specified column for (int r=0; r |
HappyPrimeTester.java
public class HappyPrimeTester { // http://en.wikipedia.org/wiki/Happy_number#Happy_primes private final static int[] happyPrimesCP = { 7, 13, 19, 23, 31, 79, 103, 109, 139, 167, 193, 239, 239, 263, 293, 313, 331, 367, 379, 409, 487 }; // I believe this is a list of only happy numbers and primes are not also included here private final static int[] onlyHappy = { 1, 7, 10, 13, 19, 23, 28, 31, 32, 44, 49, 68, 70, 79, 82, 86, 91, 94, 97, 100, 103, 109, 129, 130, 133, 139, 167, 176, 188, 190, 192, 193, 203, 208, 219, 226, 230, 236, 239, 262, 263, 280, 291, 293, 301, 302, 310, 313, 319, 320, 326, 329, 331, 338, 356, 362, 365, 367, 368, 376, 379, 383, 386, 391, 392, 397, 404, 409, 440, 446, 464, 469, 478, 487, 490, 496, 536, 556, 563, 565, 566, 608, 617, 622, 623, 632, 635, 637, 638, 644, 649, 653, 655, 656, 665, 671, 673, 680, 683, 694, 700, 709, 716, 736, 739, 748, 761, 763, 784, 790, 793, 802, 806, 818, 820, 833, 836, 847, 860, 863, 874, 881, 888, 899, 901, 904, 907, 910, 912, 913, 921, 923, 931, 932, 937, 940, 946, 964, 970, 973, 989, 998, 1000 } ; //partial list private final static int[] notHappyPrimes = {2, 3, 5, 4, 6, 8, 9, 10, 11, 12, 14, 15, 16, 17, 18, 24, 25, 26, 27, 28, 29, 30, 32, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44}; public static void main(String[] args) { //TODO: added test for new data which should be both happy and prime // The original test data using the happyPrimes array will return some false values // which is should not System.out.println("Using both happy and prime list for test"); for (int num : happyPrimesCP ) { HappyNumber hpyNum = new HappyNumber(); boolean retVal = hpyNum.isHappyPrime(num); System.out.printf("%4d: %s ", num, retVal); } //TODO: original test which will fail because test is using only happy numbers // Also corrections to the isPrime method needs to be made as discussed System.out.println("Using only happy list for test"); for (int num : onlyHappy) { HappyNumber hpyNum = new HappyNumber(); boolean retValHP = hpyNum.isHappyPrime(num); hpyNum = new HappyNumber(); boolean retValHappy = hpyNum.isHappy(num); System.out.printf("%4d: %5s %s ", num, retValHappy, retValHP); } System.out.println("Not Happy"); for (int num : notHappyPrimes ) { HappyNumber hpyNum = new HappyNumber(); boolean retVal = hpyNum.isHappyPrime(num); System.out.printf("%4d: %s ", num, retVal); } } } |
EightQueensResults.txt
0 4 7 5 2 6 1 3 row: 0, iterations: 113 1 3 5 7 2 0 6 4 row: 1, iterations: 28 2 0 6 4 7 1 3 5 row: 2, iterations: 49 3 0 4 7 1 6 2 5 row: 3, iterations: 27 4 0 3 5 7 1 6 2 row: 4, iterations: 9 5 0 4 1 7 2 6 3 row: 5, iterations: 20 6 0 2 7 5 3 1 4 row: 6, iterations: 19 7 1 3 0 6 4 2 5 row: 7, iterations: 44 |
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started