/* * Intro to Programming CSE 1310 * University of Texas at Arlington */ package spr17test2part1; /** * * @author jcmtiernan */ public class Spr17Test2Part1 { /** * @param args the command line arguments */ public static void main(String[] args) { System.out.println("Trying to find 5 digits (labeled v,a,l,u, and e) \n" + "such that (v+a+l+u+e) cubed = value."); System.out.println("The five digits must all be different."); System.out.println(""); int v=0,a=0,l=0,u=0,e=0; int value = 0; int cube = 0; int base = 0; boolean notFound = true; String sp = " "; int effCnt = 0; // First approach done in class: // Starting from 5 digit number: value // We know that the 5 digit number is between 10,000 and 99,999 // Therefore we will loop from 10K to 99999 stopping if we find // digits that work or if we reach 100K System.out.println("--As solved in class, the first solution printed--"); System.out.println("First solution: Starting from 5 digit number: value"); System.out.println("1 + 9 + 6 + 8 + 3 equals 27 and 27 cubed is 19683"); System.out.println(""); // Second approach done in class: // Starting from 5 seperate values: v,a,l,u,e // We know that each digit is a value between 0 and 9 // We will write loops to test each combination of the 5 digits // The loops stop when one value goes to 10 or when a valid // combination is found System.out.println("--As solved in class, the second solution printed--"); System.out.println("Second solution: Starting from 5 seperate values: v,a,l,u,e"); System.out.println("1 + 9 + 6 + 8 + 3 equals 27 and 27 cubed is 19683"); System.out.println(""); // Third approach: 1.a. for (base = 0; (base < 10000) && notFound; base++) { value = base*base*base; // 1.b) What is value? if ((value >= 10000) && (value < 100000)) // 1.c) Why do we test this? { cube = value; v = cube / 10000; cube = cube % 10000; a = cube / 1000; cube = cube % 1000; l = cube / 100; cube = cube % 100; u = cube / 10; e = cube % 10; if ((v+a+l+u+e) == base) // 1.d) Why do we test this? { if ((v!=a) && (v!=l) && (v!=u) && (v!=e) && (a!=l) && (a!=u) && (a!=e) && (l!=u) && (l!=e) && // XC1) If this test is true, (u!=e)) // what does it mean? notFound = false; } } effCnt++; // 1.e) What are we counting? } if (!notFound) { base--; System.out.println("Third solution: \nbase = (v+a+l+u+e)"); System.out.print(v+" + "+a+" + "+l+" + "+u+" + "+e+" equals "+base); System.out.println(" and "+base+" cubed is "+value); System.out.println(""); } else { System.out.println("No appropriate base and value was found."); } System.out.println(""); System.out.println("Comparing efficiency of the 3 approaches: "); System.out.println("\nStarting from 5 digit number: value"); System.out.println("The algorithm ran 9684 times."); System.out.println("\nStarting from 5 seperate values: v,a,l,u,e"); System.out.println("The algorithm ran 9684 times."); System.out.println("\nThird solution: "); // 1.f) What would be the value of effCntMiddle? System.out.println("The algorithm ran "+effCnt+" times."); } }