First, a few caveats:
- I didn't read the original question of the problem as stated on Project Euler
- The J code is a essentially mechanical transliteration of your Python code
- It's also effectively untested
- If you try it on your end, and it appears useful, I can write a detailed
explanation of it later today (if/when I do, I suspect the only part you'll
find surprising is the idiom ($ #: ,); the rest of the code arises from simple
substitution of J's implicit looping structures for Python's explicit loops,
which is the main attraction of J anyway)
If all that seems a little worrisome, maybe I can allay your fears by telling
you that all the caveats arise from the fact that I just woke up and picked up
my phone, and have composed and played with both the J code and this email from
a completely horizontal position*.
pe85 =: [: +/@:, */&:>:&:i.&:>:
2e6 ($@:] #: (i.<./)@:|@:(-,)) pe85"0/~ 1+ i. 200
34 75
-Dan
* In other words: I love the future! (And the J phone app.)
Please excuse typos; sent from a phone.
> On Oct 7, 2014, at 12:46 AM, Jon Hough <[email protected]> wrote:
>
> Sorry, my line breaks got deleted in the email. Her is my Python code:
>
>
> def pe85(larg, rarg): count = 0
> llist = range(1, larg+1)
> rlist = range(1, rarg+1)
>
> for l in llist:
> for r in rlist:
> count += l*r
>
> return count
>
> if __name__ == "__main__":
> # test for 2x3 grid, as in question.
> k = pe85(2,3)
> print str(k)
> l1 = range(1,200)
> l2 = range(1,200)
> bestfit = 10000
> area = 0
> for i in l1:
> for j in l2:
> diff = abs(2000000 - pe85(i,j))
> if diff < bestfit:
> area = i*j
> bestfit = diff
>
> print "AREA is "+str(area)
>> From: [email protected]
>> To: [email protected]
>> Date: Tue, 7 Oct 2014 05:37:27 +0100
>> Subject: [Jprogramming] Project Euler 85, Python and J
>>
>> Project Euler 85: https://projecteuler.net/problem=85
>> This problem is not really conceptually hard, but I am struggling with a J
>> solution.I have solved it in Python:
>> =============================================
>> def pe85(larg, rarg): count = 0 llist = range(1, larg+1) rlist =
>> range(1, rarg+1)
>> for l in llist: for r in rlist: count += l*r
>> return count
>>
>> if __name__ == "__main__": # test for 2x3 grid, as in question. k =
>> pe85(2,3) print "Test value: "+str(k) l1 = range(1,200) # 200
>> lucky guess l2 = range(1,200) bestfit = 10000 # just a big number
>> area = 0 for i in l1: for j in l2: diff = abs(2000000
>> - pe85(i,j)) if diff < bestfit: area = i*j
>> bestfit = diff
>> print "AREA is "+str(area)
>>
>>
>> ================================================The above script will give
>> the final area of the closest fit to 2 million. (The python code may not be
>> the best). Also I tested all possibilities up to 200x200, which was chosen
>> arbitrarily(~ish).
>> Next my J. I go the inner calculation ok (i.e. see the function pe85 above).
>> In J I have:
>> pe85 =: +/@:+/@:((>:@:i.@:[) *"(0 _) (>:@:i.@:]))
>> NB. I know, too brackety. Any tips for improvement appreciated.
>>
>>
>> But from here things get tricky. If I do the calculation over 200x200
>> possibilities I end up with a big matrix, of which I have to find the
>> closest value to 2 million, of which then I have to somehow get the (x,y)
>> values of and then find the area by x*y.
>>
>> The main issue is getting the (x,y) from the best fit value of the array.
>>
>> i.e. If I do pe85"(0)/~ 200, I get a big array, and I know I can get the
>> closest absolute value to 2 million but then I need to get the original
>> values to multiply together to give the best fit area. Actually I have
>> bumped into this issue many times. It is easy enough in a 1-d array,just do:
>> (I. somefunc ) { ])
>>
>> or similar to get the index. But for two indices the problem is beyond me at
>> the moment. Any help appreciated.Regards,Jon
>>
>>
>>
>> ----------------------------------------------------------------------
>> For information about J forums see http://www.jsoftware.com/forums.htm
>
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm