πŸ“œ ⬆️ ⬇️

Non-standard solution of one problem with ProjectEuler

Problems with Project Euler are good because they allow you to have fun and train your brain at different levels of complexity for the same task. The simplest approach is brute force. The first two dozen tasks for 99% are solved with this approach. After sending the correct answer to the task, the forum thread opens on it. Fans from dozens of countries are competing, who quote the solution more sophisticatedly. The more advanced use the built-in capabilities of the languages ​​in which they write the solution, but the essence is the same β€” brute force or explicit with a bunch of nested loops or implicit through the call of special functions. It looks especially beautiful in Python or Ruby languages ​​(often in one or two lines), more numerous in Java and C ++. The farther, the more forceful the "power" solutions look, using classes like BigInteger. With the increase in the number of the task, brute force can be successfully applied less and less. There are many problems on pure mathematics, where you need to solve the problem on paper, and then encode something completely simple. Sometimes you can do without writing code at all, there are many such tasks - for example, to use combinatorics.

But sometimes it is more pleasant to find a completely non-standard solution.


Example. Task 9 . Find the Pythagorean triplet of integers a, b, c, such that a + b + c = 1000. Let me remind you that the Pythagorean triple is three positive integers a <b <c that are sides of a right triangle, i.e. a ^ 2 + b ^ 2 = c ^ 2. The most famous school example: 3, 4, 5.
')
Reorientation is obvious. It is greatly reduced in time, if you know the properties of such triplets, you can save a lot of iterations. But we will go the other way.

We will use the Solver optimization tool in the MS Excel package of any version for solving.



Solver can be a very powerful tool for finding solutions, although it is said that in very multidimensional cases it succumbs. I still remember the picture when I saw a person who was looking for a Solver to solve or at least a local approximation for a huge system of equations for several dozen variables. He wrote for a master's degree, something in economics. He could definitely write code in any of several languages, but it was a scrap. I put the settings in Solver on a huge maximum number of iterations, a huge maximum search time and started on my computer at work. Something counted there as a result.

By the way, I did not manage to stick the Solver to solve the problem of the maximum sum of the path - there is a non-smooth function, random.

Several problems, as I said, managed to be solved without code, analytically. Once I had to calculate the binomial coefficient, for this I used the engineering calculator Windows. One task at the end of the first hundred was solved with the help of SQL - it was only necessary to copy the data into a table - a regular schedule in Notepad ++, creating a DDL script, and then filling in the table and query in one line.

It is also very entertaining to look at the projectEuler statistics. The most effective languages ​​and environments used are not C ++ \ Java, but the French PARI / GP, Mathematica, Python, and Haskel. Used a lot of exotic (statistics at the time of writing the post):

LanguageNumber of UsersAverage Percent SolvedScore index
onePARI / GP7923%99
2Mathematica127213%92
3Python26887eight%81
fourHaskell4680eight%67
fiveSage18012%62
6Perl2237eight%61
7C / C ++284546%61
eightAPL / J / K254eleven%60
9Java181586%58
tenC #91226%54
elevenML4369%54
12Maple2659%49
13Ruby42296%49
14Scala12707%49
15MUMPS22sixteen%49
sixteenLisp10967%48
17Delphi451eight%48
18F #9367%47
nineteenScheme7447%46
20Matlab21226%45
21Magma2115%45
22Racket1409%44
23Component pascal722%42
24BASIC11626%42
25Go4147%41
26Frinkten18%41
27Clojure9916%41
28D163eight%40
29Pencil / Paper8056%39
thirtyPascal6016%38
31Tcl659%37
32R4966%37
33RPL1414%36
34Spreadsheet4126%35
35GAP26eleven%35
36Assembly1527%34
37Lua3326%34
38Fortran3086%34
39Fororth399%32
40Smalltalk907%31
41SAS17ten%28
42Ada976%27
43ECMAScript834four%26
44Q746%25
45Erlang580four%25
46Julia367%24
47Autohotkey12ten%24
48Prolog114five%23
49Php24453%23
...............
53Octave63five%20
54Boo177%nineteen
55Cobra318%nineteen
56Logo266%nineteen
57Groovy38five%18
58SQL37five%17
59COBOLnineteen6%17
60Powershell68four%sixteen
...............
68Bourne shell323%ten
...............


Most of the people, of course, from the States - about 28 thousand, from China and Russia about the same number - about 3000. But if you poke into the country and see the top 100 for the tasks, it is clear that the Chinese are more effective - there are more people who have decided, more 400, 350, 300, etc. tasks. And in India, on the contrary, there are approximately 2.5 times more participants, but less super-solvers than in the Russian Federation. In my opinion, if we normalize this all by the number of population, then, starting with a statistically significant number of participants per country, we can get an estimate of the effectiveness of higher education in it.

Source: https://habr.com/ru/post/196966/


All Articles