Data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 7387c8f97b
commit cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions

View file

@ -0,0 +1,44 @@
import java.util.ArrayList;
import java.util.List;
// task 1
public class HailstoneSequence {
public static void main(String[] args) {
// task 2
int n = 27;
List<Long> sequence27 = hailstoneSequence(n);
System.out.printf("Hailstone sequence for %d has a length of %d:%nhailstone(%d) = %s%n", n, sequence27.size(), n, sequence27);
// task 3
int maxN = 0;
int maxLength = 0;
for ( int i = 1 ; i < 100_000 ; i++ ) {
int seqLength = hailstoneSequence(i).size();
if ( seqLength > maxLength ) {
maxLength = seqLength;
maxN = i;
}
}
System.out.printf("Longest hailstone sequence less than 100,000: hailstone(%d).length() = %d", maxN, maxLength);
}
public static List<Long> hailstoneSequence(long n) {
if ( n <= 0 ) {
throw new IllegalArgumentException("Must be grater than or equal to zero.");
}
List<Long> sequence = new ArrayList<>();
sequence.add(n);
while ( n > 1 ) {
if ( (n & 1) == 0 ) {
n /= 2;
}
else {
n = 3 * n + 1;
}
sequence.add(n);
}
return sequence;
}
}

View file

@ -0,0 +1,20 @@
import java.util.HashMap;
import java.util.Map;
import java.util.stream.IntStream;
public class ExecutableLibrary {
public static void main(String[] args) {
Map<Integer,Integer> lengthMap = new HashMap<>();
IntStream.range(1, 100_000)
.map(n -> HailstoneSequence.hailstoneSequence(n).size())
.forEach(len -> lengthMap.merge(len, 1, (v1, v2) -> v1 + v2));
int mostOften = lengthMap.values()
.stream()
.mapToInt(x -> x)
.max().orElse(-1);
System.out.printf("The most frequent hailstone length for n < 100,000 is %d.%n", mostOften);
}
}