Fork me on GitHub

hello! I'm trying to complete the armstrong numbers exercise on exercism. I think I'm almost there but can't figure out how to finish off my armstrong function. I need to replace the base parameter of my power function so I can allow input of larger numbers. If I manually put in the digits everything else checks out but can't figure out how to do it generically. Obviously this one doesn't run because there is no third function but I just wanted to show what I'm trying to achieve.

(ns armstrong-numbers)

(defn power
  "exponentation function"
  [base exp]
  (reduce * (repeat exp base)))

(defn digits
  "splits up num into digits e.g 153 becomes (1 5 3)"
  (map #(Character/digit % 10) (str number)))

(defn armstrong
  "calculates armstrong number"
  (let [exp (count (digits num))]
    (reduce + [(power (first (digits num)) exp)
               (power (second (digits num)) exp)
               (power (third (digits num)) exp)])))

(defn armstrong? [num]
  (= num (armstrong num)))


meta question: what's the best way to post long code snippets here?


change to ', or just use bigdecimals everywhere and checkout the java doc for them (they have a .pow method)


hmmm. I like the self made power function and the math works. I came across it during a different tutorial. But I'm not sure how to figure out how to take my broken up number (1 5 3) and plug it in as the base parameters in that armstrong function. I think it will have to be some kind of recursive check too right? That function is getting a little unwieldy for me to wrap my head around but can't figure out a different approach.


I may not have understood your question, I assumed you were hitting Long/MAX_LONG and getting an error due to overflow


ok, I get it


oh no, that's my fault. In that 2nd to last function armstrong where I have that vector of (power (first (digits num)) exp) I need to make the base parameter (first (digits num) into something generic(?) that will take each digit in numbers and make the vector out of them


by larger number you mean a number with more digits


You want to turn your manual calling of power on the first digit, then the second, etc in to something that does it automatically on each digit


yup! sorry about that. it needs to take any size number


so if i put in 9494 the vector will have 4 numbers calculated


so (digits num) returns a sequence of digits, do you know of a function that calls another function for each element of a sequence?


so then I should be looking into how to iterate over every number in the list i get back from digits like (1 5 3) and turn it into a vector that is [(power 1 exp) (power 5 exp) (power 3 exp)] that seems like a good approach, right?


why are you turning it in to a vector?


to be honest, i don't rightfully know. i was just experimenting in the repl and stumbled into the (reduce + (seq [(power base exp)...]) algorithm giving me the correct armstrong number calculation


isn't that seq portion redundant? lol. thats kind of embarrassing


it is, reduce (and map, and many other functions) internally call seq on collections you pass in to them


so you have map, you have a seq of digits, and you now how to sum a seq of numbers, it seems like the missing bit is actually mapping power over the seq of digits, which you cannot do directly because power takes 2 arguments and you are mapping over a single collection so your mapped function only gets a single argument


yeah, so then I'm thinking something with partial so I'm just filling in the base parameter and then the armstrong function will take care of the 2nd parameter? but now I'm getting a bit in over my head


partial is a good thought, but it doesn't exactly work, because with partial you can only partially apply to the first arguments, but you need to leave the first argument out


can you write a function that given an exp, returns a function that takes a base and returns (power base exp)


hmmm. yes, i'm going to explore that. i'm wondering if i need to just keep it in my armstrong function and figure out the base part in my let block? let me try and wrap my head around all this. thank you!


i did it! thanks so much hiredman. Not sure if I quite did what you were hinting at but it clicked something in my brain for sure. Here is the revised function:

(defn armstrong
  "calculates armstrong number"
  (let [base (digits num)
        exp (repeat (count (digits num)) (count (digits num)))]
    (reduce + (map power base exp))))


that exponent think in the let block looks a little ugly but I'm happy I actually solved it!


One neat thing about map is that it will stop at the end of the shortest of the collections it is given, so exp could be the (infinite) sequence (repeat (count (digits num))) and it would still work @chase-lambert


nice! I was thinking it could be cleaned up like that.


Hi here! I'm playing with clojure focusing on testing, I'm struggling to have a test report as a file listing the test executed, which failed and the output of the failing tests (it would be nice to have in html, or junit-xml format). I was able to generate the junit xml, but apart from the number of test executed/failed, it doesn't have any deeper detail. Do you have this kind of reports? What setup did you did to generate them (library, script, whatever)? Thank you in advance!


@manuelrascioni maybe you should look into #kaocha


I've tried with it 🙂 but when a test fails for an exception it doesn't report it in the xml


Kaocha seems what I need but can't figure out why the junit report is so "poor", I don't know if I misconfigured something or it is a common behavior


discuss in that channel or make an issue with the lib, I don’t use it myself yet


ok, thanks for the support!


Hi guys, is there a way to use pretty print as code formatter? Something akin to prettier in JS world.


If you're willing to use a third-party library for this you'll have lots of options. Here are a couple: •

💯 5

Thanks, this is that I was looking for


@marko.micic I don't know about the built in pretty-printer, but Fipp has support for formatting Clojure code. (See


Thank you, this seems useful.