Talk:Rare numbers: Difference between revisions

m
→‎Tweaks, C++: Further syntax-highlighting fixes
m (added l to htm)
m (→‎Tweaks, C++: Further syntax-highlighting fixes)
 
(8 intermediate revisions by 3 users not shown)
Line 76:
== the 1<sup>st</sup> REXX version ==
This is the 1<sup>st</sup> REXX version, &nbsp; before all the optimizations were added:
<syntaxhighlight lang="rexx">
<lang rexx>/*REXX program to calculate and display an specified amount of rare numbers. */
numeric digits 20; w= digits() + digits() % 3 /*ensure enough decimal digs for calcs.*/
parse arg many start . /*obtain optional argument from the CL.*/
Line 103 ⟶ 104:
if _>=0 then do; x= _; $= $ + q
end
end /*while q>1*/; return $</lang>
</syntaxhighlight>
Pretty simple, &nbsp; but slow as molasses in January.
 
Line 112 ⟶ 114:
of &nbsp; ''rare'' &nbsp; numbers) &nbsp; within Shyam Sunder Gupta's
<br>[http://www.shyamsundergupta.com/rare.htm <u>webpage</u>] &nbsp; have been incorporated in this REXX program.
<langsyntaxhighlight lang="rexx">/*REXX program to calculate and display an specified amount of rare numbers. */
numeric digits 20; w= digits() + digits() % 3 /*ensure enough decimal digs for calcs.*/
parse arg many start . /*obtain optional argument from the CL.*/
Line 219 ⟶ 221:
if _>=0 then do; x= _; $= $ + q
end
end /*while q>1*/; return $</langsyntaxhighlight>
Still pretty sluggish, &nbsp; like molasses in March.
 
Line 521 ⟶ 523:
square containing the combination 9 or 14 as the first/last digit.
 
<langsyntaxhighlight lang="go">package main
import (
Line 778 ⟶ 780:
bStart = time.Now() // restart block timing
}
}</langsyntaxhighlight>
{{out}}
Results on the core i7-7700 @ 3.6Ghz.
Line 862 ⟶ 864:
:I've also updated Nigel's C++ version with the same tweaks:
 
<langsyntaxhighlight lang="cpp">#include <iostream>
#include <functional>
#include <bitset>
Line 902 ⟶ 904:
int main(){
Rare(19);
}</langsyntaxhighlight>
:Compiling this with g++ brings the overall execution time down from 30 to 21 minutes in round figures. So the figures for clang or mingw may well come in at below 10 minutes now.
 
Line 922 ⟶ 924:
== Tweaks, C++ ==
Wringing out some more performance here, just over 5 minutes for 19 digits by themselves, and just over 6 minutes for digits 2 thru 19. See the code comments for details on what was tweaked. Curiously, I found that the g++ version executes faster than the clang++ version. Compiler arguments used: <code>g++ rare.cpp -o rare.exe -std=c++17 -O3</code><br/>Curious to see if it can go faster on a different platform - But the original executed faster on g++ than clang++ for me on my platform too. Perhaps I don't have clang++ set up the same as others?
<langsyntaxhighlight lang="cpp">// Rare Numbers : Nigel Galloway - December 20th., 2019
 
/*
Line 1,058 ⟶ 1,060:
printf("%4s %19s %11s %10s %5s %11s %9s\n", "nth", "forward", "rt.sum", "rt.diff", "digs", "block.et", "total.et");
for (int nd = 2; nd <= max; nd++) Rare(int(nd));
}</langsyntaxhighlight>
{{out}}
Results on the core i7-7700 @ 3.6Ghz.
Line 1,246 ⟶ 1,248:
--[[User:Nigel Galloway|Nigel Galloway]] ([[User talk:Nigel Galloway|talk]]) 14:05, 10 June 2020 (UTC)
:::Thanks for those comparisons, appreciated. Re ''3.94'', the limit is 4, I was just undercutting it a bit. --[[User:Enter your username|Enter your username]] ([[User talk:Enter your username|talk]]) 00:58, 17 June 2020 (UTC)
 
* nLH.odd / nLH.even selection based on oddness of current "outer" value, instead of the sqare itself: Okay but the odd numbers seem to be in the even bucket and the even numbers are in the odd bucket. Probably my fault for calling them odd and even since the only requirement is that the are split, not what the bucket is called, but I couldn't sleep with them the wrong way round so I've but them in the correct named bucket.--[[User:Nigel Galloway|Nigel Galloway]] ([[User talk:Nigel Galloway|talk]]) 14:09, 4 January 2021 (UTC)
 
* skip certain "outer" permutations which cannot produce squares (ends with 2, 3, 7, or 8), or will not produce squares for less than 20 digits (5 for diffs, and 9 & 14 for sums): There is no reason why 5 can not produce a rare number. 9 & 14 will not for any Rare number of any length. The situation for g0 and g1 for all rare numbers is:
<pre>
Diffs (L)
 
g0 -> g1
0 -> 0
1 -> -7..2..9
4 -> -8..2..8
5 -> -3;7
6 -> -9..2..9
 
Sums (H)
 
g0 -> g1
4 -> 0..2..18
6 -> 1..2..17
10 -> 9
11 -> 1..2..17
15 -> 1;11
16 -> 0..2..18
</pre>
--[[User:Nigel Galloway|Nigel Galloway]] ([[User talk:Nigel Galloway|talk]]) 14:09, 4 January 2021 (UTC)
* Do 2 through 11 digits by only looking at "middles" (no "outers") - accomplished by adding an additional definition for nLH() that takes only the function of optional long when doing less that 12 digits, limit diffs to be above zero, and sums to be above a limit at which smaller squares produced are less than the forward number itself (pow10[n-1] * 4) makeL(), makeH() return nLH() now, instead of nLH() components: Okay but no need for an extra global variable. It is really a parameter to nLH's constructor (single) as now only one constructor is required.--[[User:Nigel Galloway|Nigel Galloway]] ([[User talk:Nigel Galloway|talk]]) 14:09, 4 January 2021 (UTC)
* The pair "construction" shouldn't incur an overhead as the compiler should optimize it away. It adds flexability to the design, the need for which I shall reserve judgement.--[[User:Nigel Galloway|Nigel Galloway]] ([[User talk:Nigel Galloway|talk]]) 14:09, 4 January 2021 (UTC)
:::Thank you for the code review and adopting some of the tweaks I presented. Good fix for my awkward handling of digits 2-11. I agree about the pair construction, I only removed it to speed up my C# translation, which was slightly impacting performance (perhaps due to an awkward translation on my part.)<br><br>
:::I agree that digit '5' ought to be considered in the "diff" side. Even though it doesn't contribute to any solution in the ''Int64'' range, if ''Int128'' ever becomes part of C++, your program should be easily extended to the point where checking "diff 5" will contribute a solution. My decision to omit checking '5' was for performance reasons since we are constricted to ''Int64''.--[[User:Enter your username|Enter your username]] ([[User talk:Enter your username|talk]]) 00:01, 25 January 2021 (UTC)
 
== 21+ digit rare numbers ==
34

edits