Jordan-Pólya numbers: Difference between revisions

New post.
(→‎{{header|jq}}: indentation)
(New post.)
Line 483:
(,showjp) (<:3800){s
7213895789838336 24 24 24 24 24 24 24 24 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2</syntaxhighlight>
 
=={{header|Java}}==
<syntaxhighlight lang=java>
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.stream.Collectors;
import java.util.stream.Stream;
 
public final class JordanPolyaNumbers {
 
public static void main(String[] aArgs) {
createJordanPolya();
final long hundredMillionth = jordanPolyaSet.floor(100_000_000L);
List<Long> jordanPolya = new ArrayList<Long>(jordanPolyaSet);
System.out.println("The first 50 Jordan-Polya numbers:");
for ( int i = 0; i < 50; i++ ) {
System.out.print(String.format("%5s%s", jordanPolya.get(i), ( i % 10 == 9 ? "\n" : "" )));
}
System.out.println();
System.out.println("Largest Jordan-Polya number less than 100 million: " + commatise(hundredMillionth));
System.out.println();
for ( int i : List.of( 800, 1800, 2800, 3800 ) ) {
System.out.println("The " + i + "th Jordan-Polya number is: " + commatise(jordanPolya.get(i - 1))
+ " = " + toString(decompositions.get(jordanPolya.get(i - 1))));
}
}
private static void createJordanPolya() {
Set<Long> nextSet = new TreeSet<Long>();
decompositions.put(1L, new TreeMap<Integer, Integer>());
long factorial = 1;
for ( int multiplier = 2; multiplier <= 20; multiplier++ ) {
factorial *= multiplier;
for ( Iterator<Long> iterator = jordanPolyaSet.iterator(); iterator.hasNext(); ) {
long number = iterator.next();
while ( number <= Long.MAX_VALUE / factorial ) {
long original = number;
number *= factorial;
nextSet.add(number);
decompositions.put(number, new TreeMap<Integer, Integer>(decompositions.get(original)));
decompositions.get(number).merge(multiplier, 1, Integer::sum);
}
}
jordanPolyaSet.addAll(nextSet);
nextSet.clear();
}
}
private static String toString(Map<Integer, Integer> aMap) {
String result = "";
for ( int key : aMap.keySet() ) {
result = key + "!" + ( aMap.get(key) == 1 ? "" :"^" + aMap.get(key) ) + " * " + result;
}
return result.substring(0, result.length() - 3);
}
private static String commatise(long aNumber) {
String result = String.valueOf(aNumber);
for ( int i = result.length() - 3; i >= 1; i -= 3 ) {
result = result.substring(0, i) + "," + result.substring(i);
}
return result;
}
 
private static TreeSet<Long> jordanPolyaSet = Stream.of( 1L ).collect(Collectors.toCollection(TreeSet::new));
private static Map<Long, Map<Integer, Integer>> decompositions = new HashMap<Long, Map<Integer, Integer>>();
 
}
</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
 
Largest Jordan-Polya number less than 100 million: 99,532,800
 
The 800th Jordan-Polya number is: 18,345,885,696 = 4!^7 * 2!^2
The 1800th Jordan-Polya number is: 9,784,472,371,200 = 6!^2 * 4!^2 * 2!^15
The 2800th Jordan-Polya number is: 439,378,587,648,000 = 14! * 7!
The 3800th Jordan-Polya number is: 7,213,895,789,838,336 = 4!^8 * 2!^16
</pre>
 
=={{header|jq}}==
871

edits