Jump to content

Esthetic numbers: Difference between revisions

Add swift
mNo edit summary
(Add swift)
Line 5,217:
123454567 123456543 123456545 123456565 123456567 123456765 123456767 123456787 123456789
</pre>
 
=={{header|Swift}}==
 
<lang swift>extension Sequence {
func take(_ n: Int) -> [Element] {
var res = [Element]()
 
for el in self {
guard res.count != n else {
return res
}
 
res.append(el)
}
 
return res
}
}
 
extension String {
func isEsthetic(base: Int = 10) -> Bool {
zip(dropFirst(0), dropFirst())
.lazy
.allSatisfy({ abs(Int(String($0.0), radix: base)! - Int(String($0.1), radix: base)!) == 1 })
}
}
 
func getEsthetics(from: Int, to: Int, base: Int = 10) -> [String] {
guard base >= 2, to >= from else {
return []
}
 
var start = ""
var end = ""
 
repeat {
if start.count & 1 == 1 {
start += "0"
} else {
start += "1"
}
} while Int(start, radix: base)! < from
 
let digiMax = String(base - 1, radix: base)
let lessThanDigiMax = String(base - 2, radix: base)
var count = 0
 
repeat {
if count != base - 1 {
end += String(count + 1, radix: base)
count += 1
} else {
if String(end.last!) == digiMax {
end += lessThanDigiMax
} else {
end += digiMax
}
}
} while Int(end, radix: base)! < to
 
if Int(start, radix: base)! >= Int(end, radix: base)! {
return []
}
 
var esthetics = [Int]()
 
func shimmer(_ n: Int, _ m: Int, _ i: Int) {
if (n...m).contains(i) {
esthetics.append(i)
} else if i == 0 || i > m {
return
}
 
let d = i % base
let i1 = i &* base &+ d &- 1
let i2 = i1 &+ 2
 
if (i1 < i || i2 < i) {
// overflow
return
}
 
switch d {
case 0: shimmer(n, m, i2)
case base-1: shimmer(n, m, i1)
case _:
shimmer(n, m, i1)
shimmer(n, m, i2)
}
}
 
for digit in 0..<base {
shimmer(Int(start, radix: base)!, Int(end, radix: base)!, digit)
}
 
return esthetics.filter({ $0 <= to }).map({ String($0, radix: base) })
}
 
for base in 2...16 {
let esthetics = (0...)
.lazy
.map({ String($0, radix: base) })
.filter({ $0.isEsthetic(base: base) })
.dropFirst(base * 4)
.take((base * 6) - (base * 4) + 1)
 
print("Base \(base) esthetics from \(base * 4) to \(base * 6)")
print(esthetics)
print()
}
 
let base10Esthetics = (1000...9999).filter({ String($0).isEsthetic() })
 
print("\(base10Esthetics.count) esthetics between 1000 and 9999:")
print(base10Esthetics)
print()
 
func printSlice(of array: [String]) {
print(array.take(5))
print("...")
print(Array(array.lazy.reversed().take(5).reversed()))
print("\(array.count) total\n")
}
 
print("Esthetics between \(Int(1e8)) and \(13 * Int(1e7)):")
printSlice(of: getEsthetics(from: Int(1e8), to: 13 * Int(1e7)))
 
print("Esthetics between \(Int(1e11)) and \(13 * Int(1e10))")
printSlice(of: getEsthetics(from: Int(1e11), to: 13 * Int(1e10)))
 
print("Esthetics between \(Int(1e14)) and \(13 * Int(1e13)):")
printSlice(of: getEsthetics(from: Int(1e14), to: 13 * Int(1e13)))
 
print("Esthetics between \(Int(1e17)) and \(13 * Int(1e16)):")
printSlice(of: getEsthetics(from: Int(1e17), to: 13 * Int(1e16)))</lang>
 
{{out}}
 
<pre style="height: 500px">Base 2 esthetics from 8 to 12
["10101010", "101010101", "1010101010", "10101010101", "101010101010"]
 
Base 3 esthetics from 12 to 18
["1210", "1212", "2101", "2121", "10101", "10121", "12101"]
 
Base 4 esthetics from 16 to 24
["323", "1010", "1012", "1210", "1212", "1232", "2101", "2121", "2123"]
 
Base 5 esthetics from 20 to 30
["323", "343", "432", "434", "1010", "1012", "1210", "1212", "1232", "1234", "2101"]
 
Base 6 esthetics from 24 to 36
["343", "345", "432", "434", "454", "543", "545", "1010", "1012", "1210", "1212", "1232", "1234"]
 
Base 7 esthetics from 28 to 42
["345", "432", "434", "454", "456", "543", "545", "565", "654", "656", "1010", "1012", "1210", "1212", "1232"]
 
Base 8 esthetics from 32 to 48
["432", "434", "454", "456", "543", "545", "565", "567", "654", "656", "676", "765", "767", "1010", "1012", "1210", "1212"]
 
Base 9 esthetics from 36 to 54
["434", "454", "456", "543", "545", "565", "567", "654", "656", "676", "678", "765", "767", "787", "876", "878", "1010", "1012", "1210"]
 
Base 10 esthetics from 40 to 60
["454", "456", "543", "545", "565", "567", "654", "656", "676", "678", "765", "767", "787", "789", "876", "878", "898", "987", "989", "1010", "1012"]
 
Base 11 esthetics from 44 to 66
["456", "543", "545", "565", "567", "654", "656", "676", "678", "765", "767", "787", "789", "876", "878", "898", "89a", "987", "989", "9a9", "a98", "a9a", "1010"]
 
Base 12 esthetics from 48 to 72
["543", "545", "565", "567", "654", "656", "676", "678", "765", "767", "787", "789", "876", "878", "898", "89a", "987", "989", "9a9", "9ab", "a98", "a9a", "aba", "ba9", "bab"]
 
Base 13 esthetics from 52 to 78
["545", "565", "567", "654", "656", "676", "678", "765", "767", "787", "789", "876", "878", "898", "89a", "987", "989", "9a9", "9ab", "a98", "a9a", "aba", "abc", "ba9", "bab", "bcb", "cba"]
 
Base 14 esthetics from 56 to 84
["565", "567", "654", "656", "676", "678", "765", "767", "787", "789", "876", "878", "898", "89a", "987", "989", "9a9", "9ab", "a98", "a9a", "aba", "abc", "ba9", "bab", "bcb", "bcd", "cba", "cbc", "cdc"]
 
Base 15 esthetics from 60 to 90
["567", "654", "656", "676", "678", "765", "767", "787", "789", "876", "878", "898", "89a", "987", "989", "9a9", "9ab", "a98", "a9a", "aba", "abc", "ba9", "bab", "bcb", "bcd", "cba", "cbc", "cdc", "cde", "dcb", "dcd"]
 
Base 16 esthetics from 64 to 96
["654", "656", "676", "678", "765", "767", "787", "789", "876", "878", "898", "89a", "987", "989", "9a9", "9ab", "a98", "a9a", "aba", "abc", "ba9", "bab", "bcb", "bcd", "cba", "cbc", "cdc", "cde", "dcb", "dcd", "ded", "def", "edc"]
 
61 esthetics between 1000 and 9999:
[1010, 1012, 1210, 1212, 1232, 1234, 2101, 2121, 2123, 2321, 2323, 2343, 2345, 3210, 3212, 3232, 3234, 3432, 3434, 3454, 3456, 4321, 4323, 4343, 4345, 4543, 4545, 4565, 4567, 5432, 5434, 5454, 5456, 5654, 5656, 5676, 5678, 6543, 6545, 6565, 6567, 6765, 6767, 6787, 6789, 7654, 7656, 7676, 7678, 7876, 7878, 7898, 8765, 8767, 8787, 8789, 8987, 8989, 9876, 9878, 9898]
 
Esthetics between 100000000 and 130000000:
["101010101", "101010121", "101010123", "101012101", "101012121"]
...
["123456567", "123456765", "123456767", "123456787", "123456789"]
126 total
 
Esthetics between 100000000000 and 130000000000
["101010101010", "101010101012", "101010101210", "101010101212", "101010101232"]
...
["123456787878", "123456787898", "123456789876", "123456789878", "123456789898"]
911 total
 
Esthetics between 100000000000000 and 130000000000000:
["101010101010101", "101010101010121", "101010101010123", "101010101012101", "101010101012121"]
...
["123456789898767", "123456789898787", "123456789898789", "123456789898987", "123456789898989"]
6225 total
 
Esthetics between 100000000000000000 and 130000000000000000:
["101010101010101010", "101010101010101012", "101010101010101210", "101010101010101212", "101010101010101232"]
...
["123456789898987878", "123456789898987898", "123456789898989876", "123456789898989878", "123456789898989898"]
44744 total</pre>
 
=={{header|Wren}}==
Cookies help us deliver our services. By using our services, you agree to our use of cookies.