Data update

This commit is contained in:
Ingy döt Net 2025-02-27 18:35:13 -05:00
parent 8e4e15fa56
commit 72eb4943cb
1853 changed files with 35514 additions and 9441 deletions

View file

@ -1,23 +1,31 @@
public class Pancake {
private static int pancake(int n) {
int gap = 2;
int sum = 2;
int adj = -1;
while (sum < n) {
adj++;
gap = 2 * gap - 1;
sum += gap;
}
return n + adj;
}
public static void main(String[] args) {
for (int i = 0; i < 4; i++) {
for (int j = 1; j < 6; j++) {
int n = 5 * i + j;
System.out.printf("p(%2d) = %2d ", n, pancake(n));
public final class PancakeNumbersApproximation {
public static void main(String[] args) {
for ( int i = 0; i < 4; i++ ) {
for ( int j = 1; j < 6; j++ ) {
final int n = 5 * i + j;
System.out.print(String.format("%s%2d%s%d%s", "p(", n, ") = ", pancake(n), "\t"));
}
System.out.println();
}
}
private static int pancake(int number) {
int gap = 2;
int previousGap = 1;
int sumGaps = gap;
int adjustment = -1;
while ( sumGaps < number ) {
adjustment += 1;
final int previousGapCopy = previousGap;
previousGap = gap;
gap += previousGapCopy;
sumGaps += gap;
}
number += adjustment;
return number;
}
}

View file

@ -1,47 +1,53 @@
import static java.util.Comparator.comparing;
import static java.util.stream.Collectors.toList;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public final class PancakeNumbers {
public class Pancake {
public static void main(String[] args) {
for ( int n = 1; n <= 9; n++ ) {
PancakeInfo result = pancake(n);
System.out.println(String.format("%s%2d%s", "Pancake(", result.index, "). Example " + result.pancake));
}
}
private static PancakeInfo pancake(int number) {
List<Integer> initialStack = IntStream.rangeClosed(1, number).boxed().collect(Collectors.toList());
Map<List<Integer>, Integer> stackFlips = new HashMap<List<Integer>, Integer>();
stackFlips.put(initialStack, 0);
Queue<List<Integer>> queue = new ArrayDeque<List<Integer>>();
queue.add(initialStack);
while ( ! queue.isEmpty() ) {
List<Integer> stack = queue.remove();
private static List<Integer> flipStack(List<Integer> stack, int spatula) {
List<Integer> copy = new ArrayList<>(stack);
Collections.reverse(copy.subList(0, spatula));
final int flipCount = stackFlips.get(stack) + 1;
for ( int i = 2; i <= number; i++ ) {
List<Integer> flipped = flipStack(stack, i);
if ( stackFlips.putIfAbsent(flipped, flipCount) == null ) {
queue.add(flipped);
}
}
}
Map.Entry<List<Integer>, Integer> entry =
stackFlips.entrySet().stream().max(Comparator.comparing( e -> e.getValue() )).get();
return new PancakeInfo(entry.getKey(), entry.getValue());
}
private static List<Integer> flipStack(List<Integer> stack, int index) {
List<Integer> copy = new ArrayList<Integer>(stack);
Collections.reverse(copy.subList(0, index));
return copy;
}
private static record PancakeInfo(List<Integer> pancake, int index) {}
private static Map.Entry<List<Integer>, Integer> pancake(int n) {
List<Integer> initialStack = IntStream.rangeClosed(1, n).boxed().collect(toList());
Map<List<Integer>, Integer> stackFlips = new HashMap<>();
stackFlips.put(initialStack, 1);
Queue<List<Integer>> queue = new ArrayDeque<>();
queue.add(initialStack);
while (!queue.isEmpty()) {
List<Integer> stack = queue.remove();
int flips = stackFlips.get(stack) + 1;
for (int i = 2; i <= n; ++i) {
List<Integer> flipped = flipStack(stack, i);
if (stackFlips.putIfAbsent(flipped, flips) == null) {
queue.add(flipped);
}
}
}
return stackFlips.entrySet().stream().max(comparing(e -> e.getValue())).get();
}
public static void main(String[] args) {
for (int i = 1; i <= 10; ++i) {
Map.Entry<List<Integer>, Integer> result = pancake(i);
System.out.printf("pancake(%s) = %s. Example: %s\n", i, result.getValue(), result.getKey());
}
}
}