Jump to content

Arithmetic numbers: Difference between revisions

New post.
(Nim solution)
(New post.)
Line 1,036:
+/0=1 p: (1e6 {. examples) -. 1
905043</syntaxhighlight>
 
=={{header|Java}}==
<syntaxhighlight lang="java">
 
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
 
public final class ArithmeticNumbers {
 
public static void main(String[] aArgs) {
int arithmeticCount = 0;
int compositeCount = 0;
int n = 1;
while ( arithmeticCount <= 1_000_000 ) {
Set<Integer> factors = factors(n);
final int sum = factors.stream().mapToInt(Integer::intValue).sum();
if ( sum % factors.size() == 0 ) {
arithmeticCount += 1;
if ( factors.size() > 2 ) {
compositeCount += 1;
}
if ( arithmeticCount <= 100 ) {
System.out.print(String.format("%3d%s", n, ( arithmeticCount % 10 == 0 ) ? "\n" : " "));
}
if ( List.of( 1_000, 10_000, 100_000, 1_000_000 ).contains(arithmeticCount) ) {
System.out.println();
System.out.println(arithmeticCount + "th arithmetic number is " + n);
System.out.println("Number of composite arithmetic numbers <= " + n + ": " + compositeCount);
}
}
n += 1;
}
}
private static Set<Integer> factors(int aNumber) {
Set<Integer> result = Stream.of(1, aNumber).collect(Collectors.toCollection(HashSet::new));
int i = 2;
int j;
while ( ( j = aNumber / i ) >= i ) {
if ( i * j == aNumber ) {
result.add(i);
result.add(j);
}
i += 1;
}
return result;
}
 
}
</syntaxhighlight>
{{ out }}
<pre>
1 3 5 6 7 11 13 14 15 17
19 20 21 22 23 27 29 30 31 33
35 37 38 39 41 42 43 44 45 46
47 49 51 53 54 55 56 57 59 60
61 62 65 66 67 68 69 70 71 73
77 78 79 83 85 86 87 89 91 92
93 94 95 96 97 99 101 102 103 105
107 109 110 111 113 114 115 116 118 119
123 125 126 127 129 131 132 133 134 135
137 138 139 140 141 142 143 145 147 149
 
1000th arithmetic number is 1361
Number of composite arithmetic numbers <= 1361: 782
 
10000th arithmetic number is 12953
Number of composite arithmetic numbers <= 12953: 8458
 
100000th arithmetic number is 125587
Number of composite arithmetic numbers <= 125587: 88219
 
1000000th arithmetic number is 1228663
Number of composite arithmetic numbers <= 1228663: 905043
</pre>
 
=={{header|jq}}==
897

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.