Last active
October 15, 2025 05:12
-
-
Save sogaiu/764b433752d99eb92557ea7ce9fe2316 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn num-digits | |
[x] | |
(if (= x 0) | |
1 | |
(math/floor (inc (math/log10 x))))) | |
(comment | |
(num-digits 0) | |
# => | |
1 | |
(num-digits 1) | |
# => | |
1 | |
(num-digits 2) | |
# => | |
1 | |
(num-digits 9) | |
# => | |
1 | |
(num-digits 10) | |
# => | |
2 | |
(num-digits 11) | |
# => | |
2 | |
(num-digits 19) | |
# => | |
2 | |
(num-digits 20) | |
# => | |
2 | |
(num-digits 99) | |
# => | |
2 | |
(num-digits 100) | |
# => | |
3 | |
(num-digits 101) | |
# => | |
3 | |
) | |
# | |
(defn digits | |
[x] | |
(def n-dig (num-digits x)) | |
(var remaining x) | |
(def results @[]) | |
(loop [i :down-to [(dec n-dig) 0]] | |
(def pow-ten (math/pow 10 i)) | |
(def digit (div remaining pow-ten)) | |
(array/push results digit) | |
(-= remaining (* digit pow-ten))) | |
results) | |
(comment | |
(digits 123) | |
# => | |
@[1 2 3] | |
(digits 0) | |
# => | |
@[0] | |
(digits 100) | |
# => | |
@[1 0 0] | |
(digits 300) | |
# => | |
@[3 0 0] | |
(digits (* 300 300 300)) | |
# => | |
@[2 7 0 0 0 0 0 0] | |
) | |
(defn digits-to-num | |
[digs] | |
(var total 0) | |
(def num-digs (length digs)) | |
(loop [i :range [0 num-digs] | |
:let [exponent (- (dec num-digs) i) | |
pow-ten (math/pow 10 exponent) | |
digit (get digs i) | |
value (* pow-ten digit)]] | |
(+= total value)) | |
total) | |
(comment | |
(digits-to-num @[1 2 3]) | |
# => | |
123 | |
(digits-to-num @[0]) | |
# => | |
0 | |
) | |
(defn do-problem | |
[start end] | |
(seq [i :range-to [start end] | |
:let [cubed (* i i i) | |
digs (digits cubed) | |
last-three (array/slice digs -4 -1) | |
added (digits-to-num last-three)] | |
:when (= added i)] | |
[i cubed])) | |
(comment | |
(do-problem 100 300) | |
# => | |
@[[125 1953125] | |
[249 15438249] | |
[251 15813251]] | |
(do-problem 100 1000) | |
# => | |
@[[125 1953125] | |
[249 15438249] | |
[251 15813251] | |
[375 52734375] | |
[376 53157376] | |
[499 124251499] | |
[501 125751501] | |
[624 242970624] | |
[625 244140625] | |
[749 420189749] | |
[751 423564751] | |
[875 669921875] | |
[999 997002999]] | |
) | |
(defn do-n-problem | |
[start end n] | |
(seq [i :range-to [start end] | |
:let [cubed (* i i i) | |
digs (digits cubed) | |
last-n (array/slice digs (- (inc n)) -1) | |
added (digits-to-num last-n)] | |
:when (= added i)] | |
[i cubed])) | |
(comment | |
(do-n-problem 100 300 3) | |
# => | |
@[[125 1953125] | |
[249 15438249] | |
[251 15813251]] | |
(do-n-problem 100 1000 3) | |
# => | |
@[[125 1953125] | |
[249 15438249] | |
[251 15813251] | |
[375 52734375] | |
[376 53157376] | |
[499 124251499] | |
[501 125751501] | |
[624 242970624] | |
[625 244140625] | |
[749 420189749] | |
[751 423564751] | |
[875 669921875] | |
[999 997002999]] | |
(do-n-problem 100 10000 4) | |
# => | |
@[[624 242970624] | |
[625 244140625] | |
[1249 1948441249] | |
[3751 52776573751] | |
[4375 83740234375] | |
[4999 124925014999] | |
[5001 125075015001] | |
[5625 177978515625] | |
[6249 244023456249] | |
[8751 670151588751] | |
[9375 823974609375] | |
[9376 824238309376] | |
[9999 999700029999]] | |
) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment