[Jprogramming] arithmetic sequence

Boyko Bantchev boykobb at gmail.com
Sat Dec 22 15:52:31 UTC 2012


On 21 December 2012 01:02, Skip Cave <skip at caveconsulting.com> wrote:
> The best way to answer his question is to program the problem using his
> explicit looping approach, and compare that approach with the approaches
> proposed by the various members of the forum.

Comparing solutions with respect to, say, speed, may only tell us
which one to use (once we have them all!) if we want that speed.
But such a comparison does not answer Alessandro's question, namely,
whether array-based solutions, or a particular one of them, avoid
the redundant computations that he wrote of.

Furthermore, comparing solutions with explicit loops to solutions
without them always tends to be unfair to the former. Huge differences
in speed do arise not only because J performs excelently on whole-
array operations, but also because it is very slow when it has to
execute code repeatedly.  In this it is much, much slower than most
other languages.

This is easily demonstrated by writing in another language a program
similar to the above discussed acx, such as the following program in
Ruby:

n = 5000
a,b = 1,0
r = [[a,b]]
(n-1).times do
  a,b = 5*a,5*b+3
  r << [a,b]
end

Regarding performance, Ruby is well known for the lack of it.  Still,
this program is faster by orders of magnitude than acx.  It is also
faster than hr1 and hr2, although not much faster – only about twice
on my computer for n=5000.  (The speed ratio increases with the growth
of n.)

One more observation.  How can one *predict* that the closed-formula
based solution of Bo and Linda will lose so dramatically to Henry's
solutions?


More information about the Programming mailing list