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 the
problems are often posed in a way that makes brute-force computer solutions
impractical, even when using fairly powerful machines. This forces Project
Euler 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 in
working 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 brute
force, if all you have is pencil and paper.
The idea is usually to study the problem and come up with a simple scheme
that
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 in
his 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 that
solving selected Quora problems in J would be an excellent way for me to
improve my skills using tacit J. I believe my conjecture has been validated
by 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 of
Project
Euler problems*, while you can (sometimes) explore the foothills of the
challenge,
including example solutions provided, with a brute-force approach, it is
generally
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 problem
in his
High 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 from
each 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 using
a "brute force" approach, and ignore simplifying formulas. In the days
before modern computers, formulas were developed to make computations on
sequences like this Quora problem more tractable, particularly when the
number of terms got large.
Today, with powerful modern computers and languages, it is often easier to
simply generate a sequence in it's entirety, and then compute some
property
of 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 the
sequence 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 have
a 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
462
My main dissatisfaction was that I was sure that this could be done in a
single line, but I didn't know how to get the integer sequence on both
sides of the tally/copy verb, which i wanted to use as a binary selector
mask.
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
then
sum them, but I wanted to learn how to get the same vector of integers in
two 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 to
generate the selector mask and make the selections.
+/(*0=2&|)i.43
462
Raul's multiply by zero trick was cool, but my purist sensibilities still
wanted to use selection of the even numbers as the most straightforward
way
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
462
That worked, So we should also be able to get the sum of the odd numbers:
+/(#2&|)i.43
441
Yep. That worked too. So i should also be able use the NOT verb ".-" to
invert the selection mask to get the sum of evens like I did in my
original
attempt:
+/(#-.2&|)i.43
43
AAAAK! What happened? I tried to negate the selection mask, and something
went terribly wrong! Maybe I need to isolate the negation:
+/(#(-.2&|))i.43
|length error
| +/ (#(-.2&|))i.43
Nope! 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