My solution was similar:
NB. Part 1
NB. The sum-of-divisors problem
2900000 >:@]^:(> >:@#.~/.~&.q:)^:_ (1)
NB. Part 2.
sumoflargedivisors =: ([: +/ <.@%&50@<: ((< # ]) >) [: ,@:(*/)&.>/ [:
<.@(^ i.@>:)&.>/ __&q:)
29000000 >:@]^:(> 11 * sumoflargedivisors)^:_ (2)
Henry Rich
On 12/20/2015 5:26 PM, 'Pascal Jasmin' via Programming wrote:
div=: /:~ @: , @: > @: (*/&.>/) @: ((^ i.@>:)&.>/) @: (__&q:)
p:1
p1 =. >:^:(3600000> +/@:div"0)^:(_) 100000
p:2
:^:(36000000 > 11 * +/@:(] (] #~ 50 >: <.@%) div@]))^:_ p1
an interesting part about timing part 2, is that typically I would not use ^:_
when writting the function out of fear that a bug causes an infinite loop
using ^:200000 for example gets the same result as ^:_ , but timings:
timespacex '>:^:(36000000 > 11 * +/@:(] (] #~ 50 >: <.@%) div@]))^:_ p1'
0.68445 23424
timespacex '>:^:(36000000 > 11 * +/@:(] (] #~ 50 >: <.@%) div@]))^:200000
p1'
5.31956 21376
The reason its much longer is that the while condition is evaluated 200k times
even if the result has converged.
A trick that adds a small complication, but keeps the speed, is to use a simple
transform to signal an end of loop.
timespacex '0&,^:(36000000 <: 11 * +/@:(] (] #~ 50 >: <.@%)
div@]))@>:^:(1=#)^:200000 p1'
0.76082 23168
main condition is turned from while to until, and the response is to alter shape. This
shape altering signal is also useful when applying to multiple items, and 1 item can
"find a solution" and halt the entire program.
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm