10
$\begingroup$
Total[TakeWhile[Map[#^3 &, Range[1, 200]], # < 10000 &]]
$\endgroup$
8
  • 4
    $\begingroup$ Could you also explain in words what you are trying to achieve? $\endgroup$ Commented Aug 16, 2016 at 8:44
  • $\begingroup$ Total@Select[Range[200]^3, # < 10000 &] $\endgroup$ Commented Aug 16, 2016 at 8:46
  • $\begingroup$ 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 $\endgroup$
    – bios
    Commented Aug 16, 2016 at 8:48
  • 1
    $\begingroup$ Total @ TakeWhile[ Range[ 200 ]^3, LessThan[ 10000 ] ] looks decent. $\endgroup$
    – gwr
    Commented Aug 16, 2016 at 8:49
  • $\begingroup$ @Quantum_Oli Select only works here because the list ist ordered, but in general is not the same as TakeWhile... $\endgroup$
    – gwr
    Commented Aug 16, 2016 at 8:51

7 Answers 7

20
$\begingroup$
Total[Range[CubeRoot[10000]]^3]

53361

$\endgroup$
8
  • $\begingroup$ Actually Tr[Range[CubeRoot[10000]]^3] as @Kuba had written is faster. $\endgroup$
    – gwr
    Commented 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) 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. $\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$
    – gwr
    Commented 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 than Tr ;) $\endgroup$
    – Karsten7
    Commented Aug 16, 2016 at 20:42
8
$\begingroup$

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...

$\endgroup$
3
  • $\begingroup$ This is the most satisfactory answer so far. $\endgroup$
    – bios
    Commented Aug 16, 2016 at 9:03
  • 2
    $\begingroup$ @bios you can reflect that by upvoting (gray triangle next to the answer) $\endgroup$
    – Kuba
    Commented 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$
    – gwr
    Commented Aug 16, 2016 at 9:34
5
$\begingroup$
NestWhile[{#[[1]] + 1, #[[1]]^3, #[[2]] + #[[3]]} &, {1, 0, 0}, #[[2]] < 10000 &] // Last

53361

$\endgroup$
7
  • 1
    $\begingroup$ Why not NestWhile[{#1 + 1, #1^3, #2 + #3} & @@ # &, {1, 0, 0}, #[[2]] < 10000 &] // Last? $\endgroup$
    – xyz
    Commented Aug 16, 2016 at 9:21
  • $\begingroup$ Nice making use of functional programming - albeit not very readable for humans imo. :) $\endgroup$
    – gwr
    Commented Aug 16, 2016 at 9:31
  • $\begingroup$ @ShutaoTang Why Slot and Apply instead of Part? $\endgroup$
    – Karsten7
    Commented Aug 16, 2016 at 9:31
  • $\begingroup$ @gwr That's subjective. $\endgroup$
    – Karsten7
    Commented Aug 16, 2016 at 9:34
  • $\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$
    – gwr
    Commented Aug 16, 2016 at 9:37
5
$\begingroup$

Why are people not making use of listability?

Total[Range[Floor[1*^4^(1/3)]]^3]
$\endgroup$
2
  • 2
    $\begingroup$ Why was this answer downvoted? I agree it's similar to @nikie's but, there are many similar ones here. $\endgroup$
    – Feyre
    Commented Aug 16, 2016 at 9:49
  • 1
    $\begingroup$ Floor should be redundant and then it IS @nikie's answer? $\endgroup$
    – gwr
    Commented Aug 16, 2016 at 17:48
4
$\begingroup$

Or like this:

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

Total[%]
(*53361*)
$\endgroup$
4
$\begingroup$

I don't think you'll beat

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

for speed. It did require some thought though.

$\endgroup$
4
$\begingroup$

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 *)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.