Another approach to Euler 44:
NB. make a pentagonal number generator verb:
*pn=.3 :'y*(1-~3*y)%2'*
NB. Generate the first 5000 pentagonal numbers and store them in p. Then
find all possible pair combinations of the first 5000 pentagonal numbers
and store the pairs in p2. Then store the two integers in each pair in the
vectors a & b respectively:
* 'a b'=.|:p2=.(2 comb 5000){p=:pn>:i.5000x*
NB. Find and mark all pairs where a+b and a-b are both pentagonal numbers.
Store that mark vector in m. Also sum the marks in m to see how many
solution pairs we have:
* +/m=.(p e. ~a+b) *. p e. ~|a-b*
*1*
NB. So there is only one solution pair in the first 5000 pentagonal
numbers. Now use the mark vector to extract that one pair of pentagonal
numbers we found that meets all the criteria, store the pair in n, and
display them.
* ]n=.m#p2*
*1560090 7042750*
NB. List a, b, a+b, & a-b of the discovered pair in n=a,b:
* (,n),(+/"1 n),(|-/"1 n) *
*1560090 7042750 8602840 5482660*
NB. Are the four integers a, b, a+b, & a-b all pentagonal numbers?
*p e.~(,n),(+/"1 n),(|-/"1 n) *
*1 1 1 1*
NB. Yes. So what is "D" (the difference between the pair) which is required
to get the final answer in the euler question?
*]D=.|-/"1 n*
*5482660*
However the Euler 44 question wants to find a pentagonal pair where D is
minimised, so we will need to expand our search so see if there are
pentagonal pairs with a smaller D. Unfortunately, I run into memory limits
when I try to examine a larger range of pentagonal numbers. So I'll need to
break the one-shot approach into a loop and segment the array of pentagonal
numbers (sigh). I'll leave that exercise for the reader...
Skip Cave
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm