Don
On 2017-08-30 6:47 AM, 'Mike Day' via Programming wrote:
Good points, Skip.Mind you, I've learnt (and then forgotten again) a lot of J AND maths doing Euler probs.Keep sharing, Thanks, Mike On 29/08/2017 20:48, Skip Cave wrote:Project Euler problems typically assume the use of a computer. Thus theproblems are often posed in a way that makes brute-force computer solutions impractical, even when using fairly powerful machines. This forces ProjectEuler problem-solvers to seek algorithmic approaches that avoid large amounts of memory and/or compute time. Thus Project Euler solutions emphasize the discovery of novel (not-brute-force) algorithms, to minimize computational overhead.However, discovering compute-optimal algorithms was not my primary goal inworking on Quora problems. My goal was to improve my proficiency in J, particularly tacit J.Quora problems are typically constructed to be difficult to solve by bruteforce, if all you have is pencil and paper. The idea is usually to study the problem and come up with a simple schemethat can achieve the answer, just using pencil and paper. This is much like Project Euler, but the limit with Quora is space on your paper, and your personal time to perform the individual manual calculations. Roger posted a perfect example of this issue with his story about Euler inhis math class, solving the sum of a sequence. Quora problems can quite often be solved using a brute force approach,which can be constructed using a matrix language like J. My idea was thatsolving selected Quora problems in J would be an excellent way for me toimprove my skills using tacit J. I believe my conjecture has been validatedby the array of diverse solutions presented by forum members, and my resulting improved understanding of J. Skip Skip Cave Cave Consulting LLC On Tue, Aug 29, 2017 at 1:28 PM, 'Mike Day' via Programming < [email protected]> wrote:At the risk of getting too chatty, I'd point out that in the majority ofProjectEuler problems*, while you can (sometimes) explore the foothills of thechallenge,including example solutions provided, with a brute-force approach, it isgenerally necessary to find ways to avoid requiring too much of either or both memory and compute time.BTW, I recall the story that Gauss answered the sum of integers problemin hisHigh School class, by considering, say, the sequence 1 2 3 ... 20 21, and its reverse, 21 20 ... 3 2 1, showing that each pair of items fromeach sums to 22, so that the sum of the two sequences is 21 * 22... No doubt apocryhal. Cheers, Mike * see https://projecteuler.net On 29/08/2017 09:04, Skip Cave wrote:There seems to be two basic approaches to this problem: 1. Generate the even numbers between 1 & 42, and add them up. 2. Use the formula for the sum of an arithmetic progression - e.g.: Sum=n(a+b)/2 *n* = number of numbers in the sequence (here 21) *a* = the first number in the sequence (here 2) *b* = the last number in the sequence (here 42)Generally, I like to approach these kinds of Quora sequence problems usinga "brute force" approach, and ignore simplifying formulas. In the daysbefore modern computers, formulas were developed to make computations on sequences like this Quora problem more tractable, particularly when thenumber of terms got large.Today, with powerful modern computers and languages, it is often easier tosimply generate a sequence in it's entirety, and then compute some propertyof that sequence such as it's limit, sum, etc. to get the final result.This is particularly true when you have at your fingertips a powerful matrix language such as APL or J. For me, the obvious approach to this problem is to simply generate thesequence of even integers and then sum them, instead of trying to find a formula that relates to that specific sequence (in any case, I didn't havea clue where to look for such formulas). So the plan was: Generate the numbers from 0 to 42, throw out the odd numbers (zero doesn't matter in this problem), and then add up what's left: a =:i.43 +/(-.2|a)#a 462My main dissatisfaction was that I was sure that this could be done in asingle line, but I didn't know how to get the integer sequence on bothsides of the tally/copy verb, which i wanted to use as a binary selectormask. Yes, thanks to Raul and others, I now realize that I could have just multiplied the first 21 integers by 2 to get all the even integers and thensum them, but I wanted to learn how to get the same vector of integers intwo places in the formula without having to assign that vector to a variable.Raul showed me how to do that, though he used equality & multiplication togenerate the selector mask and make the selections. +/(*0=2&|)i.43 462Raul's multiply by zero trick was cool, but my purist sensibilities still wanted to use selection of the even numbers as the most straightforwardway to implement the sum of evens. However, Raul's approach did show me the way. I could use the copy operator to implement the selector mask, instead of multiplication +/(#0=2&|)i.43 462That worked, So we should also be able to get the sum of the odd numbers:+/(#2&|)i.43 441Yep. That worked too. So i should also be able use the NOT verb ".-" toinvert the selection mask to get the sum of evens like I did in my original attempt: +/(#-.2&|)i.43 43AAAAK! What happened? I tried to negate the selection mask, and somethingwent terribly wrong! Maybe I need to isolate the negation: +/(#(-.2&|))i.43 |length error | +/ (#(-.2&|))i.43Nope! What am I doing wrong? How can I simply negate the selection mask,just like I did in my original explicit example? Skip Cave ---------------------------------------------------------------------- For information about J forums see http://www.jsoftware.com/forums.htm--- This email has been checked for viruses by Avast antivirus software. https://www.avast.com/antivirus ---------------------------------------------------------------------- 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
