10
Total[TakeWhile[Map[#^3 &, Range[1, 200]], # < 10000 &]]
LCarvalho
  • 9,233
  • 4
  • 40
  • 96
bios
  • 401
  • 2
  • 9
  • 4
    Could you also explain in words what you are trying to achieve? – Quantum_Oli Aug 16 '16 at 08:44
  • Total@Select[Range[200]^3, # < 10000 &] – Quantum_Oli Aug 16 '16 at 08: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 08:48
  • 1
    Total @ TakeWhile[ Range[ 200 ]^3, LessThan[ 10000 ] ] looks decent. – gwr Aug 16 '16 at 08: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 08:51
  • Hi gwr, thank you for your help. I would like to ask when we can use "@" after a function. For instance, The function "Total" followed by a "@". I understand it means we apply the list to the "Total". How can we do t the same thing to the TakeWhile. The problem is that TakeWhile needs two arguments. – bios Aug 16 '16 at 08:57
  • @gwr Good point! Thanks! – Quantum_Oli Aug 16 '16 at 09:20
  • @bios: f@x basically means the same as f[x], it's just a shorthand. Here's a great overview: http://mathematica.stackexchange.com/questions/18393/what-are-the-most-common-pitfalls-awaiting-new-users/25616#25616 – Niki Estner Aug 16 '16 at 10:24

7 Answers7

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

53361

Niki Estner
  • 36,101
  • 3
  • 92
  • 152
8

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

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

53361

Karsten7
  • 27,448
  • 5
  • 73
  • 134
5

Why are people not making use of listability?

Total[Range[Floor[1*^4^(1/3)]]^3]
gwr
  • 13,452
  • 2
  • 47
  • 78
4

Or like this:

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

Total[%]
(*53361*)
xyz
  • 605
  • 4
  • 38
  • 117
4

I don't think you'll beat

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

for speed. It did require some thought though.

mikado
  • 16,741
  • 2
  • 20
  • 54
4

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 *)
Simon Woods
  • 84,945
  • 8
  • 175
  • 324