Rare numbers: Difference between revisions

→‎{{header|Go}}: Some improvements - reduces execution time by about a third.
(→‎{{header|Go}}: Some improvements - reduces execution time by about a third.)
Line 74:
 
=={{header|Go}}==
As in the case of the REXX entry, thisThis takes into account all the hints within Shyam Sunder Gupta's webpage. It also makes use of his observation that no rare number below 10^20 ends in 3 and uses a bit arithmetic trick to further filter out numbers that cannot be perfect squares and thereby avoid the expensive math.Sqrt operation.
 
Despite all this, it's still prettyquite slow, polishing off the first five rare numbers in about 10.175 seconds but taking almostmore 19than 12.5 minutes to find the next three. Timings are for anHowever, Intelmemory Coreusage i7-8565Uis machineminimal.
 
Timings are for an Intel Core i7-8565U machine.
<lang go>package main
 
Line 196 ⟶ 198:
digits[i] = 0
}
switch a {
case 3:
digits[ai], digits[qi] = 4, 0
Line 208 ⟶ 210:
digits[ai] = 2
}
a = digits[ai]
}
b,if pqi-ai :=> digits[ai+1],2 digits[qi-1]{
switchb, p := digits[qiai+1], {digits[qi-1]
switch a {
case 2: switch a {
if b != p {
continue
}
case 4:
if (b-p)%2 != 0 {
continue
}
case 6:
if (b-p)%2 == 0 {
continue
}
case 8:
switch digits[qi] {
case 2:
if b+p !=< 9p {
continue
} else if b > p {
digits[qi-1] = b
}
case 34:
if b == p || (b > p && (b-p)%2 != 7) || (b < p && (b-p) != -3)0 {
continueif p < 9 {
digits[qi-1]++
} else {
continue
}
}
case 76:
if (b > 1 && (b+-p)%2 != 11) || (b < 1 && (b+p) != 1)0 {
continueif p < 9 {
digits[qi-1]++
} else {
continue
}
}
case 8:
ifswitch b != pdigits[qi] {
case continue2:
if (b-+p)%2 =!= 09 {
if p < 9-b {
digits[qi-1] = 9 - b
} else {
continue
}
}
case 47:
if b > 1 && (b+p) != 11 {
if p < 11-b {
digits[qi-1] = 11 - b
} else {
continue
}
} else if b < 1 && (b+p) != 1 {
if p == 0 {
digits[qi-1] = 1
} else {
continue
}
}
case 68:
if b !=< p {
continue
} else if b > p {
if (b-p)%2 !== 0 {
digits[qi-1] = b
} else {
continue
}
}
}
}
Line 291 ⟶ 321:
if sr*sr != n-r {
continue
}
count++
fmt.Printf(" %d: %15s\n", count, commatize(n))
Line 298 ⟶ 328:
}
}
fmt.Printf("\nTook %s\n", time.Since(start))
}</lang>
 
{{outoutput}}
<pre>
The first 8 rare numbers are:
Line 313 ⟶ 343:
8: 872,568,754,178
 
Took 18m5012m43.767540147s807949011s
</pre>
 
9,492

edits