# Is there a more elegant way to write this statement?

Total[TakeWhile[Map[#^3 &, Range[1, 200]], # < 10000 &]]

• Could you also explain in words what you are trying to achieve? – Quantum_Oli Aug 16 '16 at 8:44
• Total@Select[Range[200]^3, # < 10000 &] – Quantum_Oli Aug 16 '16 at 8:46
• we apply (ˆ3) to an infinite list (sorry, I do not know how to write an infinite list, so I wrote a big number 200). and then once an element that;s over 100000 is encountered, the list is cut off. Finally, we sum it up – bios Aug 16 '16 at 8:48
• Total @ TakeWhile[ Range[ 200 ]^3, LessThan[ 10000 ] ] looks decent. – gwr Aug 16 '16 at 8:49
• @Quantum_Oli Select only works here because the list ist ordered, but in general is not the same as TakeWhile... – gwr Aug 16 '16 at 8:51

Total[Range[CubeRoot[10000]]^3]


53361

• Actually Tr[Range[CubeRoot[10000]]^3] as @Kuba had written is faster. – gwr Aug 16 '16 at 15:38
• @gwr: I see no measurable difference. I'm almost certain they end up calling the same low-level functions. But if you want speed, use Total[Range[CubeRoot[10000.]]^3] (notice the . after the 10000) or Total[Range[CubeRoot[N[10000]]]^3] - that way the whole calculation is done in machine precision floating point numbers, which is faster by an order of magnitude. – Niki Estner Aug 16 '16 at 17:55
• Use RepeatedTiming and a sufficiently large number. The difference also is detectable when using machine precision. – gwr Aug 16 '16 at 18:03
• @gwr: What is sufficiently large? I've tried 60s, and I see no difference. Have you tried repeating your measurement? – Niki Estner Aug 16 '16 at 18:09
• @gwr Just change the order and Total will look better than Tr ;) – Karsten 7. Aug 16 '16 at 20:42

I like compositions for readability, thus:

Range[200] // RightComposition[
# ^ 3 &,
TakeWhile[#, LessThan @ 10000] &,
Total
]


53361

Using Composition also works (here in infix form):

Total @* (TakeWhile[ #, LessThan@10000] &) @ (Range[200]^3)


Note, that using Composition in its infix form reveals somen tricky precendence issues. Thus use expr // Defer // FullForm and compare what happens if parantheses are dropped and if Superscript-Power-notation is used rather than ^3...

• This is the most satisfactory answer so far. – bios Aug 16 '16 at 9:03
• @bios you can reflect that by upvoting (gray triangle next to the answer) – Kuba Aug 16 '16 at 9:23
• It may be old fashioned to not write everything as a one liner, but this form allows for putting a comment behind every function telling what is going on as it gets a separate line. After all, humans are a special form of parser... – gwr Aug 16 '16 at 9:34
NestWhile[{#[[1]] + 1, #[[1]]^3, #[[2]] + #[[3]]} &, {1, 0, 0}, #[[2]] < 10000 &] // Last


53361

• Why not NestWhile[{#1 + 1, #1^3, #2 + #3} & @@ # &, {1, 0, 0}, #[[2]] < 10000 &] // Last? – xyz Aug 16 '16 at 9:21
• Nice making use of functional programming - albeit not very readable for humans imo. :) – gwr Aug 16 '16 at 9:31
• @ShutaoTang Why Slot and Apply instead of Part? – Karsten 7. Aug 16 '16 at 9:31
• @gwr That's subjective. – Karsten 7. Aug 16 '16 at 9:34
• Most certainly it is. But I would bet that having people with different backgrounds in computing vote on it would reveal a majority on my side of the argument. (cf. my comment on my own answer) – gwr Aug 16 '16 at 9:37

Why are people not making use of listability?

Total[Range[Floor[1*^4^(1/3)]]^3]

• Why was this answer downvoted? I agree it's similar to @nikie's but, there are many similar ones here. – Feyre Aug 16 '16 at 9:49
• Floor should be redundant and then it IS @nikie's answer? – gwr Aug 16 '16 at 17:48

Or like this:

Map[#^3 &, Range[1, Floor[10000 ^(1/3)]]]

Total[%]
(*53361*)


I don't think you'll beat

(# (# + 1)/2)^2& @ Floor[CubeRoot[10000]]


for speed. It did require some thought though.

Going for variety more than elegance here:

{0, 0} //. {{x_, y_} :> {x + y^3, y + 1} /; y^3 < 10000, {x_, _} :> x}
(* 53361 *)

If[#2^3 < 10000, #0[#1 + #2^3, #2 + 1], #1] &[0, 0]
(* 53361 *)

1 ~Range~ 200 ~Power~ 3 ~TakeWhile~ LessThan@10000 ~Total~ 1
(* 53361 *)