I am profiting from a foray into Python, which, it is claimed, is much more
easily understood than J. Some people boast they can program in Python as
quickly as they can type.

Problem 1 of Project Euler:

Find the sum of all the multiples of 3 or 5 below 1000.

Programming in J, I like to see intermediate results to be sure I am on the
right lines, so I write a simple important useful function

   will_divide_into=: +./ @: (0 = |/)
   3 5 will_divide_into i.10
1 0 0 1 0 1 1 0 0 1
   3 5 (will_divide_into # ]) i.10
0 3 5 6 9
   3 5 ([: +/ will_divide_into # ]) i.10
23

When I have got here, I am pretty confident to get the answer as

   3 5 ([: +/ will_divide_into # ]) i.1000
233168

If you are pretty competent at Python you can type this:

>>> ans = 0
>>> for n in range(1000):
        if not n % 3 or not n % 5:
                ans = ans + n

                
>>> ans
233168
>>> 

Very different in concept! The J code will easily handle any number of
divisors, whereas the Python code I have written wouldn't comfortably. Now,
I prefer the J code because I understand it, and because I understand the
power and the potential of the concepts, but I can see why ordinary people
instinctively vote for Python.

----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to