Rare numbers: Difference between revisions
→{{header|Go}}: Replaced Cartesian product with a recursive loop, unified the two previous versions and extended Rare number generation to 18 digits.
(→Version 2: Replaced with version which has relatively low memory requirement.) |
(→{{header|Go}}: Replaced Cartesian product with a recursive loop, unified the two previous versions and extended Rare number generation to 18 digits.) |
||
Line 119:
=={{header|Go}}==
This uses many of the hints within Shyam Sunder Gupta's webpage combined with Nigel Galloway's general approach (see Talk page) of working from (n-r) and deducing the Rare numbers with various numbers of digits from there.
As the algorithm used does not generate the Rare numbers in order, a sorted list is also printed.
<lang go>package main
Line 152 ⟶ 148:
sum = sum*10 + int64(digits[i])
}
}
return sum
Line 167 ⟶ 153:
func isSquare(n int64) bool {
if 0x202021202030213&(1<<(n&63)) != 0 {
root := int64(math.Sqrt(float64(n)))
return root*root == n
}
return false
}
Line 226 ⟶ 180:
start := time.Now()
pow := int64(1)
fmt.Println("Aggregate timings to process all numbers up to:")
// terms of (n-r) expression for number of digits from 2 to maxDigits
allTerms := make([][]term, maxDigits-1)
Line 254 ⟶ 209:
}
fl := []int8{0, 1, 4, 6}
dl := seq(-9, 9, 1) // all differences
zl := []int8{0} // zero differences only
el := seq(-8, 8, 2) // even differences only
ol := seq(-9, 9, 2) // odd differences only
il := seq(0, 9, 1)
var rares []int64
Line 264 ⟶ 219:
lists[i] = [][]int8{{f}}
}
var digits []int8
count := 0
// Recursive closure to generate (n+r) candidates from (n-r) candidates
// and hence find Rare numbers with a
var fnpr func(cand, di []int8, dis [][]int8, indices [][2]int8, nmr int64, nd, level int)
fnpr = func(cand, di []int8, dis [][]int8, indices [][2]int8, nmr int64, nd, level int) {
if level
digits[indices[0][0]]
digits[indices[0][1]] =
le
if nd%2 == 1
}
r
npr := nmr +
if
}
count++
fmt.Printf(" R/N
fmt.Printf(" %9s ms", commatize(time.Since(start).Milliseconds()))
n := toInt64(digits,
fmt.Printf(" (%s)\n",
rares = append(rares,
}
for _, num := range
fnpr(cand, di, dis, indices,
}
}
}
// Recursive closure to generate (n-r) candidates with a given number of digits.
var fnmr func(cand []int8, list [][]int8, indices [][2]int8, nd, level int)
fnmr = func(cand []int8, list [][]int8, indices [][2]int8, nd, level int) {
if nmr <= 0 || !isSquare(nmr) {
dis = append(dis, seq(0, int8(len(fml[cand[0]]))-1, 1))
for i := 1; i < len(cand); i++ {
dis = append(dis, seq(0, int8(len(dmd[cand[i]]))-1, 1))
}
if nd%2 == 1 {
dis = append(dis, il)
}
di := make([]int8, len(dis))
fnpr(cand, di, dis, indices, nmr, nd, 0)
} else {
for _, num := range list[level] {
cand[level] = num
fnmr(cand, list, indices, nd, level+1)
}
}
}
for nd := 2; nd <= maxDigits; nd++ {
digits
if nd == 4 {
lists[0] = append(lists[0], zl)
Line 513 ⟶ 302:
indices = append(indices, [2]int8{t.ix1, t.ix2})
}
for _, list := range lists {
}
fmt.Printf(" %2d digits: %9s ms\n", nd, commatize(time.Since(start).Milliseconds()))
}
sort.Slice(rares, func(i, j int) bool { return rares[i] < rares[j] })
fmt.Printf("\nThe rare numbers with up to %d digits are:\n", maxDigits)
for i, rare := range rares {
fmt.Printf(" %2d: %
}
}</lang>
{{output}}
Timings are for an Intel Core i7-8565U machine with 32GB RAM running Go 1.13.1 on Ubuntu 18.04.
<pre>
Aggregate timings to process all numbers up to:
R/N 1: 0 ms (65)
2 digits: 0 ms
3 digits: 0 ms
4 digits: 0 ms
5 digits: 0 ms
R/N 8: 738 ms (868,591,084,757)
12 digits: 888 ms
R/N 9: 1,130 ms (6,979,302,951,885)
13 digits: 1,446 ms
R/N 10: 4,990 ms (20,313,693,904,202)
R/N 11: 5,058 ms (20,313,839,704,202)
R/N 12: 6,475 ms (20,331,657,922,202)
R/N 13: 6,690 ms (20,331,875,722,202)
R/N 14: 7,293 ms (20,333,875,702,202)
R/N 15: 16,685 ms (40,313,893,704,200)
R/N 16: 16,818 ms (40,351,893,720,200)
14 digits: 17,855 ms
R/N 17: 17,871 ms (200,142,385,731,002)
R/N 18: 18,079 ms (221,462,345,754,122)
R/N 19: 20,774 ms (816,984,566,129,618)
R/N 20: 22,155 ms (245,518,996,076,442)
R/N 21: 22,350 ms (204,238,494,066,002)
R/N 22: 22,413 ms (248,359,494,187,442)
R/N 23: 22,687 ms (244,062,891,224,042)
R/N 24: 26,698 ms (403,058,392,434,500)
R/N 25: 26,905 ms (441,054,594,034,340)
15 digits: 27,932 ms
R/N 26: 77,599 ms (2,133,786,945,766,212)
R/N 27: 96,932 ms (2,135,568,943,984,212)
R/N 28: 99,869 ms (8,191,154,686,620,818)
R/N 29: 102,401 ms (8,191,156,864,620,818)
R/N 30: 103,535 ms (2,135,764,587,964,212)
R/N 31: 105,255 ms (2,135,786,765,764,212)
R/N 32: 109,232 ms (8,191,376,864,400,818)
R/N 33: 122,372 ms (2,078,311,262,161,202)
R/N 34: 148,814 ms (8,052,956,026,592,517)
R/N 35: 153,226 ms (8,052,956,206,592,517)
R/N 36: 185,251 ms (8,650,327,689,541,457)
R/N 37: 187,467 ms (8,650,349,867,341,457)
R/N 38: 189,163 ms (6,157,577,986,646,405)
R/N 39: 217,112 ms (4,135,786,945,764,210)
R/N 40: 230,719 ms (6,889,765,708,183,410)
16 digits: 231,583 ms
R/N 41: 236,505 ms (86,965,750,494,756,968)
R/N 42: 237,391 ms (22,542,040,692,914,522)
R/N 43: 351,728 ms (67,725,910,561,765,640)
17 digits: 360,678 ms
R/N 44: 392,403 ms (284,684,666,566,486,482)
R/N 45: 513,738 ms (225,342,456,863,243,522)
R/N 46: 558,603 ms (225,342,458,663,243,522)
R/N 47: 653,047 ms (225,342,478,643,243,522)
R/N 48: 718,569 ms (284,684,868,364,486,482)
R/N 49: 1,087,602 ms (871,975,098,681,469,178)
R/N 50: 1,763,809 ms (865,721,270,017,296,468)
R/N 51: 1,779,059 ms (297,128,548,234,950,692)
R/N 52: 1,787,466 ms (297,128,722,852,950,692)
R/N 53: 1,888,803 ms (811,865,096,390,477,018)
R/N 54: 1,940,347 ms (297,148,324,656,930,692)
R/N 55: 1,965,331 ms (297,148,546,434,930,692)
R/N 56: 2,273,287 ms (898,907,259,301,737,498)
R/N 57: 2,657,073 ms (631,688,638,047,992,345)
R/N 58: 2,682,636 ms (619,431,353,040,136,925)
R/N 59: 2,948,725 ms (619,631,153,042,134,925)
R/N 60: 3,011,962 ms (633,288,858,025,996,145)
R/N 61: 3,077,937 ms (633,488,632,647,994,145)
R/N 62: 3,928,545 ms (653,488,856,225,994,125)
R/N 63: 4,195,016 ms (497,168,548,234,910,690)
18 digits: 4,445,897 ms
The rare numbers with up to
1: 65
2: 621,770
3: 281,089,082
4: 2,022,652,202
5: 2,042,832,002
6: 868,591,084,757
7: 872,546,974,178
8: 872,568,754,178
9: 6,979,302,951,885
10: 20,313,693,904,202
11: 20,313,839,704,202
12: 20,331,657,922,202
13: 20,331,875,722,202
14: 20,333,875,702,202
15: 40,313,893,704,200
16: 40,351,893,720,200
17: 200,142,385,731,002
18: 204,238,494,066,002
19: 221,462,345,754,122
20: 244,062,891,224,042
21: 245,518,996,076,442
22: 248,359,494,187,442
23: 403,058,392,434,500
24: 441,054,594,034,340
25: 816,984,566,129,618
26: 2,078,311,262,161,202
27: 2,133,786,945,766,212
28: 2,135,568,943,984,212
29: 2,135,764,587,964,212
30: 2,135,786,765,764,212
31: 4,135,786,945,764,210
32: 6,157,577,986,646,405
33: 6,889,765,708,183,410
34: 8,052,956,026,592,517
35: 8,052,956,206,592,517
36: 8,191,154,686,620,818
37: 8,191,156,864,620,818
38: 8,191,376,864,400,818
39: 8,650,327,689,541,457
40: 8,650,349,867,341,457
41: 22,542,040,692,914,522
42: 67,725,910,561,765,640
43: 86,965,750,494,756,968
44: 225,342,456,863,243,522
45: 225,342,458,663,243,522
46: 225,342,478,643,243,522
47: 284,684,666,566,486,482
48: 284,684,868,364,486,482
49: 297,128,548,234,950,692
50: 297,128,722,852,950,692
51: 297,148,324,656,930,692
52: 297,148,546,434,930,692
53: 497,168,548,234,910,690
54: 619,431,353,040,136,925
55: 619,631,153,042,134,925
56: 631,688,638,047,992,345
57: 633,288,858,025,996,145
58: 633,488,632,647,994,145
59: 653,488,856,225,994,125
60: 811,865,096,390,477,018
61: 865,721,270,017,296,468
62: 871,975,098,681,469,178
63: 898,907,259,301,737,498
</pre>
|