[Jprogramming] J v Python

Don & Cathy Kelly dhky at shaw.ca
Fri Dec 7 22:15:12 UTC 2012


What I find with the Python version is that with very little 
modification this could have been  written in Fortran or Basic back in 
the early '70s with essentially the same statements- a "for" and an" if."
. Whoopee!
You could also do this with J using control structures and could show it 
to your scoffers-pointing out its limitations -a need to change code if 
the numbers change.

You can also show different approaches such as

+/~.((0=3|y) +0=5|y)#y=:i.1000

233168
while this is cruder than  your version the steps may be easy to explain.
i.e. what i. y does-
what # does with a boolean left argument
What 3|y (residue operator does and how one can select only the 0 
elements by 0=
  Now -since we are getting the sum of 2 lists, multiples of both 3 and 
5 will be repeated so now
explain ~.
Finally the +/
pretty well all of these operations would require loops in Python or 
whatever.





Then point out what your solution can do that theirs (and the above)  
cannot, without rewriting the code.

That is rather than 3 and 5 and the total range being specified in the 
code, your approach can do more
It could be  a verb  "findmultiples"  used as "v findmultiples range" 
where v is , in this case v is 3 5 and range is 1000 (or 1001 if you 
want to include the value 1000.
Initially it might  have to be a highly commented program with 
statements such as sum=:+/  ,  nub=:~.   etc
but a final version could be a concise one liner.

Just a comment from a learner

Don Kelly


---------------------------------------------------------------------------- 
On 06/12/2012 10:41 AM, Graham Parkhouse wrote:
>> 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
>



More information about the Programming mailing list