solution.scm 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. ;;; Power digit sum
  2. ;;; Problem 16
  3. ;;; 2^15 = 32768 and the sum of its digits is 3 + 2 + 7 + 6
  4. ;;; + 8 = 26.
  5. ;;; What is the sum of the digits of the number 2^1000?
  6. ;; IDEA 1: Maybe the supposed problem is, that 2 to the
  7. ;; power of 1000 is a too large number for some programming
  8. ;; languages, to contain in their standard number types?
  9. ;; IDEA 2: Maybe the supposed problem is, that
  10. ;; exponentiating by 1000 takes a long time? Maybe the idea
  11. ;; is to use fast exponentiation?
  12. ;; IDEA 3: Maybe there is a pattern in the digits sum
  13. ;; depending on the exponent?
  14. ;; 0 -> 1 (double)
  15. ;; 1 -> 2 (double)
  16. ;; 2 -> 4 (double)
  17. ;; 3 -> 8 (double)
  18. ;; 4 -> 7 (minus 1)
  19. ;; 5 -> 5 (minus 2)
  20. ;; 6 -> 10 (double)
  21. ;; 7 -> 11 (plus 1)
  22. ;; 8 -> 13 (plus 2)
  23. ;; 9 -> 8 (minus 5)
  24. ;; 10 -> 7 (minus 1)
  25. (import
  26. (except (rnrs base) let-values map)
  27. (only (guile)
  28. lambda* λ
  29. ;; printing
  30. display
  31. simple-format)
  32. (lib math))
  33. ;; Alright, just gonna calculate it in a naive way. No idea
  34. ;; what insight I am missing out on.
  35. (display
  36. (simple-format
  37. #f "~a\n"
  38. (digits-sum (expt 2 1000))))