Total[TakeWhile[Map[#^3 &, Range[1, 200]], # < 10000 &]]
Asked
Active
Viewed 2,035 times
10
7 Answers
20
Total[Range[CubeRoot[10000]]^3]
53361
Niki Estner
- 36,101
- 3
- 92
- 152
-
-
@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. – Niki Estner Aug 16 '16 at 17:55 -
Use
RepeatedTimingand 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 The performance of
Trvs.Totalis version dependent. I don't see a difference: screen snippet – Karsten7 Aug 16 '16 at 19:12 -
-
-
2
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
-
-
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
-
1Why not
NestWhile[{#1 + 1, #1^3, #2 + #3} & @@ # &, {1, 0, 0}, #[[2]] < 10000 &] // Last? – xyz Aug 16 '16 at 09:21 -
Nice making use of functional programming - albeit not very readable for humans imo. :) – gwr Aug 16 '16 at 09:31
-
-
-
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 09:37
-
-
@ShutaoTang It looks much nicer in the FrontEnd using matching double brackets screen snippet. I'm just not a big fan of extra
@@ # &. – Karsten7 Aug 16 '16 at 09:47
5
Why are people not making use of listability?
Total[Range[Floor[1*^4^(1/3)]]^3]
gwr
- 13,452
- 2
- 47
- 78
Joe Shaka Khul
- 61
- 1
-
2Why was this answer downvoted? I agree it's similar to @nikie's but, there are many similar ones here. – Feyre Aug 16 '16 at 09:49
-
1
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
Total@Select[Range[200]^3, # < 10000 &]– Quantum_Oli Aug 16 '16 at 08:46Total @ TakeWhile[ Range[ 200 ]^3, LessThan[ 10000 ] ]looks decent. – gwr Aug 16 '16 at 08:49Selectonly works here because the list ist ordered, but in general is not the same asTakeWhile... – gwr Aug 16 '16 at 08:51f@xbasically means the same asf[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