Total[TakeWhile[Map[#^3 &, Range[1, 200]], # < 10000 &]]
7 Answers
Total[Range[CubeRoot[10000]]^3]
53361
-
$\begingroup$ Actually
Tr[Range[CubeRoot[10000]]^3]
as @Kuba had written is faster. $\endgroup$– gwrCommented Aug 16, 2016 at 15:38 -
$\begingroup$ @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) orTotal[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. $\endgroup$ Commented Aug 16, 2016 at 17:55 -
$\begingroup$ Use
RepeatedTiming
and a sufficiently large number. The difference also is detectable when using machine precision. $\endgroup$– gwrCommented Aug 16, 2016 at 18:03 -
$\begingroup$ @gwr: What is sufficiently large? I've tried 60s, and I see no difference. Have you tried repeating your measurement? $\endgroup$ Commented Aug 16, 2016 at 18:09
-
2$\begingroup$ @gwr Just change the order and
Total
will look better thanTr
;) $\endgroup$– Karsten7Commented Aug 16, 2016 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
...
-
$\begingroup$ This is the most satisfactory answer so far. $\endgroup$– biosCommented Aug 16, 2016 at 9:03
-
2$\begingroup$ @bios you can reflect that by upvoting (gray triangle next to the answer) $\endgroup$– KubaCommented Aug 16, 2016 at 9:23
-
$\begingroup$ 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... $\endgroup$– gwrCommented Aug 16, 2016 at 9:34
NestWhile[{#[[1]] + 1, #[[1]]^3, #[[2]] + #[[3]]} &, {1, 0, 0}, #[[2]] < 10000 &] // Last
53361
-
1$\begingroup$ Why not
NestWhile[{#1 + 1, #1^3, #2 + #3} & @@ # &, {1, 0, 0}, #[[2]] < 10000 &] // Last
? $\endgroup$– xyzCommented Aug 16, 2016 at 9:21 -
$\begingroup$ Nice making use of functional programming - albeit not very readable for humans imo. :) $\endgroup$– gwrCommented Aug 16, 2016 at 9:31
-
$\begingroup$ @ShutaoTang Why
Slot
andApply
instead ofPart
? $\endgroup$– Karsten7Commented Aug 16, 2016 at 9:31 -
-
$\begingroup$ 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) $\endgroup$– gwrCommented Aug 16, 2016 at 9:37
Why are people not making use of listability?
Total[Range[Floor[1*^4^(1/3)]]^3]
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 *)
Total@Select[Range[200]^3, # < 10000 &]
$\endgroup$Total @ TakeWhile[ Range[ 200 ]^3, LessThan[ 10000 ] ]
looks decent. $\endgroup$Select
only works here because the list ist ordered, but in general is not the same asTakeWhile
... $\endgroup$