Jordan-Pólya numbers: Difference between revisions
Content added Content deleted
(Add Python implementation) |
(Add Kotlin implementation) |
||
Line 1,049: | Line 1,049: | ||
The 3800th Jordan-Pólya number is: 7213895789838336 |
The 3800th Jordan-Pólya number is: 7213895789838336 |
||
= 4!⁸ x 2!¹⁶ |
= 4!⁸ x 2!¹⁶ |
||
</pre> |
|||
=={{header|Kotlin}}== |
|||
{{trans|Java}} |
|||
<syntaxhighlight lang="Kotlin"> |
|||
import java.util.* |
|||
object JordanPolyaNumbers { |
|||
private val jordanPolyaSet = TreeSet<Long>() |
|||
private val decompositions = HashMap<Long, TreeMap<Int, Int>>() |
|||
@JvmStatic |
|||
fun main(aArgs: Array<String>) { |
|||
createJordanPolya() |
|||
val belowHundredMillion = jordanPolyaSet.floor(100_000_000L) |
|||
val jordanPolya = ArrayList(jordanPolyaSet) |
|||
println("The first 50 Jordan-Polya numbers:") |
|||
for (i in 0 until 50) { |
|||
print(String.format("%5s%s", jordanPolya[i], if (i % 10 == 9) "\n" else "")) |
|||
} |
|||
println() |
|||
println("The largest Jordan-Polya number less than 100 million: $belowHundredMillion") |
|||
println() |
|||
for (i in listOf(800, 1050, 1800, 2800, 3800)) { |
|||
println("The $i th Jordan-Polya number is: ${jordanPolya[i - 1]} = ${toString(decompositions[jordanPolya[i - 1]]!!)}") |
|||
} |
|||
} |
|||
private fun createJordanPolya() { |
|||
jordanPolyaSet.add(1L) |
|||
val nextSet = TreeSet<Long>() |
|||
decompositions[1L] = TreeMap() |
|||
var factorial = 1L |
|||
for (multiplier in 2..20) { |
|||
factorial *= multiplier |
|||
val iterator = jordanPolyaSet.iterator() |
|||
while (iterator.hasNext()) { |
|||
var number = iterator.next() |
|||
while (number <= Long.MAX_VALUE / factorial) { |
|||
val original = number |
|||
number *= factorial |
|||
nextSet.add(number) |
|||
decompositions[number] = TreeMap(decompositions[original]!!) |
|||
decompositions[number]?.merge(multiplier, 1) { a, b -> a + b } |
|||
} |
|||
} |
|||
jordanPolyaSet.addAll(nextSet) |
|||
nextSet.clear() |
|||
} |
|||
} |
|||
private fun toString(aMap: Map<Int, Int>): String { |
|||
return aMap.entries.joinToString(separator = " * ") { (key, value) -> |
|||
"$key!${if (value == 1) "" else "^$value"}" |
|||
} |
|||
} |
|||
} |
|||
fun main(args: Array<String>) { |
|||
JordanPolyaNumbers.main(arrayOf<String>()) |
|||
} |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
The first 50 Jordan-Polya numbers: |
|||
1 2 4 6 8 12 16 24 32 36 |
|||
48 64 72 96 120 128 144 192 216 240 |
|||
256 288 384 432 480 512 576 720 768 864 |
|||
960 1024 1152 1296 1440 1536 1728 1920 2048 2304 |
|||
2592 2880 3072 3456 3840 4096 4320 4608 5040 5184 |
|||
The largest Jordan-Polya number less than 100 million: 99532800 |
|||
The 800 th Jordan-Polya number is: 18345885696 = 2!^2 * 4!^7 |
|||
The 1050 th Jordan-Polya number is: 139345920000 = 2! * 5!^3 * 8! |
|||
The 1800 th Jordan-Polya number is: 9784472371200 = 2!^15 * 4!^2 * 6!^2 |
|||
The 2800 th Jordan-Polya number is: 439378587648000 = 7! * 14! |
|||
The 3800 th Jordan-Polya number is: 7213895789838336 = 2!^16 * 4!^8 |
|||
</pre> |
</pre> |
||