Advertisement
thecrypticace

Javascript Solution to that awesome problem

Jun 27th, 2012
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.05 KB | None | 0 0
  1. Javascript solution, slightly brute force:
  2.  
  3. function sumOfDigitsOfNumbers(n1, n2) {
  4. return (n1+"" + n2+"").split("").reduce(function(a, b){
  5. return parseInt(a, 10) + parseInt(b, 10);
  6. }, 0);
  7. }
  8.  
  9. var totalSpaces = 1;
  10. for(var y=0;y<299;++y) {
  11. for(var x=0;x<299;++x) {
  12. if (sumOfDigitsOfNumbers(x, y) > 19) break;
  13. totalSpaces += 8;
  14. }
  15. }
  16.  
  17. console.log("The total number of spaces that are accessible to the monkey are: " + totalSpaces + ". Which is, sadly, not a prime number. Bummer.");
  18.  
  19.  
  20. // Why only count values up to 298?
  21. // 298 adds to 19, so this would be the max number in any direction then we just don't count any values that make it go above 19 as it can only travel 1 space @ a time.
  22. // Theoretically 1000 is a possible number, but 299 being before it makes it unreachable
  23. // therefore: the maximal dataset consists of only 298x298 (88804) possible numbers
  24.  
  25. // This cycle exists 8 times
  26. // 1 for vertical, 1 for horizontal in 4 quadrants of the graph
  27. // 8 x eighth of the limited, walked dataset + 1 initial space = 8 x 9471 + 1 = 75769
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement