Multi-base primes: Difference between revisions

→‎{{header|Wren}}: Generalized program to deal with any base up to 62. Added results for latter.
(→‎{{header|Go}}: Generalized program to deal with any base up to 62. Added results for latter.)
(→‎{{header|Wren}}: Generalized program to deal with any base up to 62. Added results for latter.)
Line 1,275:
This takes about 1.6 seconds to process up to 4 character strings and 58 seconds for the extra credit which is not too bad for the Wren interpreter.
<lang ecmascript>import "/math" for Int, Nums
import "/fmt" for Conv
 
var maxDepth = 5
var maxBase = 36
var c = Int.primeSieve(36maxBase.pow(maxDepth), false)
var digits = Conv.digits // all digits up to base 36
var digits = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
var maxStrings = []
var mostBases = -1
 
var process = Fn.new { |indices|
var minBase = 2.max(Nums.max(indices) + 1)
if (37maxBase - minBase + 1 < mostBases) return // can't affect results so return
var bases = []
for (b in minBase..36maxBase) {
var n = 0
for (i in indices) n = n * b + i
Line 1,300:
}
}
 
var printResults = Fn.new {
System.print("%(maxStrings[0][1].count)")
Line 1,308:
}
}
 
var nestedFor // recursive
nestedFor = Fn.new { |indices, length, level|
Line 1,321:
}
}
 
for (depth in 1..maxDepth) {
System.write("%(depth) character strings which are prime in most bases: ")
Line 1,327:
mostBases = -1
var indices = List.filled(depth, 0)
nestedFor.call(indices, digits.countmaxBase, 0)
printResults.call()
System.print()
Line 1,351:
5 character strings which are prime in most bases: 18
30271 -> [8, 10, 12, 13, 16, 17, 18, 20, 21, 23, 24, 25, 31, 32, 33, 34, 35, 36]
</pre>
<br>
If we change maxBase to 62 and maxDepth to 4 in the above script, then the following results are reached in 17 seconds:
<pre>
1 character strings which are prime in most bases: 60
2 -> [3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62]
 
2 character strings which are prime in most bases: 31
65 -> [7, 8, 9, 11, 13, 14, 16, 17, 18, 21, 22, 24, 27, 28, 29, 31, 32, 37, 38, 39, 41, 42, 43, 44, 46, 48, 51, 52, 57, 58, 59]
 
3 character strings which are prime in most bases: 33
1l1 -> [22, 23, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 36, 38, 39, 40, 41, 42, 43, 44, 45, 46, 48, 51, 52, 53, 54, 57, 58, 59, 60, 61, 62]
b9b -> [13, 14, 15, 16, 17, 19, 20, 21, 23, 24, 26, 27, 28, 30, 31, 34, 36, 39, 40, 42, 45, 47, 49, 50, 52, 53, 54, 57, 58, 59, 60, 61, 62]
 
4 character strings which are prime in most bases: 32
1727 -> [8, 9, 11, 12, 13, 15, 16, 17, 19, 20, 22, 23, 24, 26, 27, 29, 31, 33, 36, 37, 38, 39, 41, 45, 46, 48, 50, 51, 57, 58, 60, 61]
417b -> [12, 13, 15, 16, 17, 18, 19, 21, 23, 25, 28, 30, 32, 34, 35, 37, 38, 39, 41, 45, 48, 49, 50, 51, 52, 54, 56, 57, 58, 59, 61, 62]
</pre>
9,479

edits