Using Roger's method of replacing 100 + i. 900 with 900 + i. 100 I see even better results and because of the nature to the algorithm even better in chunks of 50 (especially space).
timespacex ' >./ (#~ (-: |.)@":"0) , */~ 100+i. 900' 0.48508 9.46483e6 timespacex '(-: |.)@": break1 \:~ ~. , */~ 100+i.900' 0.0358972 1.78367e7 timespacex ' >./ (#~ (-: |.)@":"0) , */~ 900+i. 100' 0.00559906 151936 timespacex '(-: |.)@": break1 \:~ ~. , */~ 900+i.100' 0.00599915 790144 timespacex ' >./ (#~ (-: |.)@":"0) , */~ 950+i. 50' 0.00145386 40832 20 * 0.00145386 40832 NB. Approx. time and space if you did 100 to 999 exhaustively. Could be faster if you started from the 950 - 999 and worked your way down, stopping at the first hit. 0.0290772 816640 Cheers, bob On Jun 8, 2014, at 9:05 AM, 'Pascal Jasmin' via Programming <[email protected]> wrote: > an optimization to short circuit on first true, while sorting them in order. > > break1 =: (1 : 'for_i. y do. if. u i do. i return. end. end.') > > (-: |.)@": break1 \:~ ~. , */~ 100+i.900 > 906609 > > timespacex '(-: |.)@": break1 \:~ ~. , */~ 100+i.900' > 0.0350227 3.56737e7 > timespacex ' >./ (#~ (-: |.)@":"0) , */~ 100+i. 900' > 0.26438 1.78811e7 > > ----- Original Message ----- > From: Roger Hui <[email protected]> > To: Programming forum <[email protected]> > Cc: > Sent: Sunday, June 8, 2014 11:10:07 AM > Subject: Re: [Jprogramming] Project Euler Question 4 > > Or apply the phrase to 900+i.100 instead of to 100+i.900. > > > > > > On Sun, Jun 8, 2014 at 8:07 AM, Roger Hui <[email protected]> wrote: > >> Slightly shorter if the formatted results are not converted back into >> numbers: >> >> >./ (#~ (-: |.)@("."0@":)"0) , */~ 100+i. 900 >> 906609 >> >./ (#~ (-: |.)@":"0) , */~ 100+i. 900 >> 906609 >> >> A different and perhaps less brutish method is to start with 6-digit >> palindromes and see which ones have two 3-digits factors. >> >> >> >> >> On Sun, Jun 8, 2014 at 8:00 AM, Henry Rich <[email protected]> wrote: >> >>> Brute force seems right for this problem. You could write your solution >>> as >>> >>> >./ (#~ (-: |.)@("."0@":)"0) , */~ 100+i. 900 >>> >>> Henry Rich >>> >>> On 6/8/2014 10:46 AM, Jon Hough wrote: >>> >>>> I am pretty pleased that I completed Project Euler Q4. >>>> Question: >>>> >>>> >>>> A palindromic number reads the same both ways. The largest palindrome >>>> made from the product of two 2-digit numbers is 9009 = 91 99. >>>> >>>> Find the largest palindrome made from the product of two 3-digit numbers. >>>> >>>> >>>> https://projecteuler.net/problem=4 >>>> >>>> >>>> My solution: >>>> >>>> base =. "."0 ": >>>> >>>> NB. Tests if y value is palindrome. >>>> is_palindrome =. (# =)@:(=|.)@:base >>>> NB. multiply all 3 digit nums (100 ~ 999) >>>> mult =: (* is_palindrome"0) @ (*/) >>>> NB. Create list of 3 digit nums. >>>> list =. 100+ i.900 >>>> >>>> (>./)@:, list mult list >>>> Although I'm glad got the answer, I'm wondering if it could be made >>>> terser, or if there is a much terser way to solve it? I went for a brute >>>> force approach. >>>> For comparison here is a Haskell answer I found: >>>> maximum[a*b|a<-[100..999],b<-[a..999],reverse(show(a*b))==show(a*b)] >>>> Regards. >>>> ---------------------------------------------------------------------- >>>> 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 > > ---------------------------------------------------------------------- > For information about J forums see http://www.jsoftware.com/forums.htm ---------------------------------------------------------------------- For information about J forums see http://www.jsoftware.com/forums.htm
