{"id":48,"date":"2013-01-14T15:33:54","date_gmt":"2013-01-14T23:33:54","guid":{"rendered":"http:\/\/willclausen.com\/?p=48"},"modified":"2013-01-14T15:37:31","modified_gmt":"2013-01-14T23:37:31","slug":"solution-three-wise-men-problem-dec-28-2012","status":"publish","type":"post","link":"http:\/\/willclausen.com\/?p=48","title":{"rendered":"Solution: Three Wise Men Problem, Dec. 28, 2012"},"content":{"rendered":"<p>This solution is written in Java.<\/p>\n<p>The <a title=\"Three Wise Men Problem\" href=\"http:\/\/programmingpraxis.com\/2012\/12\/28\/three-wise-men\/\" target=\"_blank\">task<\/a>:<\/p>\n<p style=\"padding-left: 30px;\">Three wise men named Caspar, Melchior and Balthazar went to Macy\u2019s Department Store on 34th Street to buy gifts of gold, frankincense, and myrrh. When they took their gifts to the cashier, she multiplied the prices of the items and found a total price of $65.52, but the wise men noticed that she multiplied the numbers and asked her to compute the total price again, but this time using addition instead of multiplication. When she did, the cashier found that the total was exactly the same. What were the individual prices of the three gifts?<\/p>\n<p style=\"padding-left: 30px;\">Your task is to solve the puzzle and find the three prices.<\/p>\n<p>My solution:<\/p>\n<pre class=\"brush: java; light: false; title: ; toolbar: true; notranslate\" title=\"\">&lt;br \/&gt;&lt;br \/&gt;\/\/ Author: Will Clausen&lt;br \/&gt;\/\/&lt;br \/&gt;\/\/ Date: Jan 6, 2013&lt;br \/&gt;\/\/&lt;br \/&gt;\/\/ This program will solve the Three Wise Men&lt;br \/&gt;\/\/ problem from Programming Praxis&lt;br \/&gt;\/\/ http:\/\/programmingpraxis.com\/2012\/12\/28\/three-wise-men\/&lt;br \/&gt;&lt;br \/&gt;import java.util.*; \/\/ For lists&lt;br \/&gt;import java.lang.Math; \/\/ For sqrt()&lt;br \/&gt;import java.text.DecimalFormat; \/\/ For converting to dollars at the end.&lt;br \/&gt;&lt;br \/&gt;\/\/ The solution to this problem will involve factoring multiple times&lt;br \/&gt;\/\/ and determining if the sum of the factors is equal to the cost&lt;br \/&gt;&lt;br \/&gt;public class ThreeWiseMenProblem&lt;br \/&gt;{&lt;br \/&gt;\tpublic int cost;&lt;br \/&gt;\tpublic int adjustedCost;&lt;br \/&gt;\tList factors = new ArrayList();&lt;br \/&gt;&lt;br \/&gt;\tThreeWiseMenProblem(double price)&lt;br \/&gt;\t{&lt;br \/&gt;\t\t\/\/ The input to the problem will be a double representing the total&lt;br \/&gt;\t\t\/\/ cost. This needs to be adjusted to the number of pennies and then&lt;br \/&gt;\t\t\/\/ again adjusted by a factor of 100 for the purpose of factoring with&lt;br \/&gt;\t\t\/\/ pennies.&lt;br \/&gt;\t\tadjustedCost = (int) (price*10000);&lt;br \/&gt;\t\t\/\/ The cost in pennies will also be stored.&lt;br \/&gt;\t\tcost = (int) (price*100);&lt;br \/&gt;&lt;br \/&gt;\t\t\/\/ This loop produces all of possible combinations of two prices&lt;br \/&gt;\t\t\/\/ (in pennies) that when multiplied produce the total cost. The prices&lt;br \/&gt;\t\t\/\/ (factors of the cost, basically) are stored in a list to be used&lt;br \/&gt;\t\t\/\/ later.&lt;br \/&gt;\t\tfor (int i = 1; i &amp;lt;= (int) Math.sqrt((double) adjustedCost); i ++) {&lt;br \/&gt;\t\t\tif (adjustedCost % i == 0) {&lt;br \/&gt;\t\t\t\tPrice factor = new Price(i, adjustedCost\/i);&lt;br \/&gt;\t\t\t\tfactors.add(factor);&lt;br \/&gt;\t\t\t}&lt;br \/&gt;\t\t}&lt;br \/&gt;\t}&lt;br \/&gt;&lt;br \/&gt;\t\/\/ This function generates a solution to the Three Wise Men Problem.&lt;br \/&gt;\t\/\/ The helper function factor(Price) does most of the heavy lifting.&lt;br \/&gt;\tpublic Solution solve()&lt;br \/&gt;\t{&lt;br \/&gt;\t\tSolution solution = new Solution();&lt;br \/&gt;&lt;br \/&gt;\t\t\/\/ I need to loop through the list of factors and factor each&lt;br \/&gt;\t\t\/\/ price AGAIN to get three total prices.&lt;br \/&gt;\t\t\/\/ Then, I need to check the sum and If the sum is equal to the&lt;br \/&gt;\t\t\/\/ total cost, then that's the solution!&lt;br \/&gt;\t\tfor (int i = 0; i &amp;lt; factors.size(); i++) {&lt;br \/&gt;\t\t\tPrice prices = factors.get(i);&lt;br \/&gt;\t\t\tsolution = factor(prices);&lt;br \/&gt;&lt;br \/&gt;\t\t\t\/\/ If a solution is found, then return it and be done.&lt;br \/&gt;\t\t\tif ((solution.firstPrice) +&lt;br \/&gt;\t\t\t\t\t(solution.secPrice) +&lt;br \/&gt;\t\t\t\t\t(solution.thirdPrice) == ((double) cost)\/100) {&lt;br \/&gt;\t\t\t\treturn solution;&lt;br \/&gt;\t\t\t}&lt;br \/&gt;\t\t}&lt;br \/&gt;\t\t\/\/ Well, there is no solution if we get here, so just return&lt;br \/&gt;\t\t\/\/ a failed solution.&lt;br \/&gt;\t\treturn solution;&lt;br \/&gt;\t}&lt;br \/&gt;&lt;br \/&gt;\t\/\/ This helper function does the work to generate a solution for the&lt;br \/&gt;\t\/\/ Three Wise Men Problem.&lt;br \/&gt;\tpublic Solution factor(Price prices)&lt;br \/&gt;\t{&lt;br \/&gt;\t\t\/\/ First factor the first price as was done in the constructor for the&lt;br \/&gt;\t\t\/\/ ThreeWiseMenProblem class.&lt;br \/&gt;\t\tfor (int i = 1;&lt;br \/&gt;\t\t\t\ti &amp;lt;= (int) Math.sqrt((double) (prices.first*100));&lt;br \/&gt;\t\t\t\ti++) {&lt;br \/&gt;\t\t\tif (prices.first*100 % i == 0) {&lt;br \/&gt;\t\t\t\t\/\/ If the three prices add to the total cost, return this&lt;br \/&gt;\t\t\t\t\/\/ as the answer!&lt;br \/&gt;\t\t\t\tif (i + (prices.first*100\/i) + prices.second == cost) {&lt;br \/&gt;\t\t\t\t\tSolution solution =&lt;br \/&gt;\t\t\t\t\t\t\tnew Solution(i,&lt;br \/&gt;\t\t\t\t\t\t\t\t\t(prices.first*100\/i),&lt;br \/&gt;\t\t\t\t\t\t\t\t\tprices.second);&lt;br \/&gt;\t\t\t\t\treturn solution;&lt;br \/&gt;\t\t\t\t}&lt;br \/&gt;\t\t\t}&lt;br \/&gt;\t\t}&lt;br \/&gt;&lt;br \/&gt;\t\t\/\/ Now the same thing for the second price.&lt;br \/&gt;\t\tfor (int i = 1; i &amp;lt;= (int) Math.sqrt((double) (prices.second*100)); i++) {&lt;br \/&gt;\t\t\tif (prices.second*100 % i == 0) {&lt;br \/&gt;\t\t\t\t\/\/ Check the sum as before.&lt;br \/&gt;\t\t\t\tif (i + (prices.second*100\/i) + prices.first == cost) {&lt;br \/&gt;\t\t\t\t\tSolution solution =&lt;br \/&gt;\t\t\t\t\t\t\tnew Solution(i,&lt;br \/&gt;\t\t\t\t\t\t\t\t\t(prices.second*100\/i),&lt;br \/&gt;\t\t\t\t\t\t\t\t\tprices.first);&lt;br \/&gt;\t\t\t\t\treturn solution;&lt;br \/&gt;\t\t\t\t}&lt;br \/&gt;\t\t\t}&lt;br \/&gt;\t\t}&lt;br \/&gt;&lt;br \/&gt;\t\t\/\/ If no solution was found, return a solution that represents that.&lt;br \/&gt;\t\tSolution failed = new Solution(0, 0, 0);&lt;br \/&gt;\t\treturn failed;&lt;br \/&gt;\t}&lt;br \/&gt;&lt;br \/&gt;\t\/\/ This is merely a wrapper class to allow for keeping prices that are&lt;br \/&gt;\t\/\/ factors for the total cost in a list in the three wise men problem.&lt;br \/&gt;\tpublic class Price&lt;br \/&gt;\t{&lt;br \/&gt;\t\tpublic int first;&lt;br \/&gt;\t\tpublic int second;&lt;br \/&gt;&lt;br \/&gt;\t\t\/\/ The only thing needed is a constructor that takes to ints (aka prices).&lt;br \/&gt;\t\tPrice(int one, int two)&lt;br \/&gt;\t\t{&lt;br \/&gt;\t\t\tfirst = one;&lt;br \/&gt;\t\t\tsecond = two;&lt;br \/&gt;\t\t}&lt;br \/&gt;&lt;br \/&gt;\t}&lt;br \/&gt;&lt;br \/&gt;\t\/\/ A wrapper class for taking care of the three solutions&lt;br \/&gt;\tpublic class Solution&lt;br \/&gt;\t{&lt;br \/&gt;\t\tpublic double firstPrice;&lt;br \/&gt;\t\tpublic double secPrice;&lt;br \/&gt;\t\tpublic double thirdPrice;&lt;br \/&gt;&lt;br \/&gt;\t\t\/\/ A default constructor is used above and is handy for keeping the code&lt;br \/&gt;\t\t\/\/ a little cleaner.&lt;br \/&gt;\t\tSolution()&lt;br \/&gt;\t\t{&lt;br \/&gt;\t\t\tfirstPrice = 0.00;&lt;br \/&gt;\t\t\tsecPrice = 0.00;&lt;br \/&gt;\t\t\tthirdPrice = 0.00;&lt;br \/&gt;\t\t}&lt;br \/&gt;&lt;br \/&gt;\t\t\/\/ A constructor that takes the three prices (in pennies) as arguments.&lt;br \/&gt;\t\tSolution(int first, int second, int third)&lt;br \/&gt;\t\t{&lt;br \/&gt;\t\t\t\/\/ Convert the price in pennies to a price in dollars.&lt;br \/&gt;\t\t\tfirstPrice = ((double)first)\/100.0;&lt;br \/&gt;\t\t\tsecPrice = ((double)second)\/100.0;&lt;br \/&gt;\t\t\tthirdPrice = ((double)third)\/100.0;&lt;br \/&gt;\t\t}&lt;br \/&gt;\t}&lt;br \/&gt;}&lt;br \/&gt;&lt;br \/&gt;<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This solution is written in Java. The task: Three wise men named Caspar, Melchior and Balthazar went to Macy\u2019s Department Store on 34th Street to buy gifts of gold, frankincense, and myrrh. When they took their gifts to the cashier, she multiplied the prices of the items and found a total price of $65.52, but [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[13,9,11,12,15],"class_list":["post-48","post","type-post","status-publish","format-standard","hentry","category-programming-praxis","tag-december-2012","tag-january2013","tag-programming-praxis-2","tag-solution","tag-three-wise-men"],"_links":{"self":[{"href":"http:\/\/willclausen.com\/index.php?rest_route=\/wp\/v2\/posts\/48","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/willclausen.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/willclausen.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/willclausen.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/willclausen.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=48"}],"version-history":[{"count":5,"href":"http:\/\/willclausen.com\/index.php?rest_route=\/wp\/v2\/posts\/48\/revisions"}],"predecessor-version":[{"id":53,"href":"http:\/\/willclausen.com\/index.php?rest_route=\/wp\/v2\/posts\/48\/revisions\/53"}],"wp:attachment":[{"href":"http:\/\/willclausen.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=48"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/willclausen.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=48"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/willclausen.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=48"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}