Ejemplo n.º 1
0
 @Test
 public void shouldConvertToList() {
   final Value<Integer> value = of(1, 2, 3);
   final List<Integer> list = value.toList();
   if (value.isSingleValued()) {
     assertThat(list).isEqualTo(List.of(1));
   } else {
     assertThat(list).isEqualTo(List.of(1, 2, 3));
   }
 }
Ejemplo n.º 2
0
  /**
   * <strong>Problem 37 Truncatable primes</strong>
   *
   * <p>The number 3797 has an interesting property. Being prime itself, it is possible to
   * continuously remove digits from left to right, and remain prime at each stage: 3797, 797, 97,
   * and 7. Similarly we can work from right to left: 3797, 379, 37, and 3.
   *
   * <p>Find the sum of the only eleven primes that are both truncatable from left to right and
   * right to left.
   *
   * <p>NOTE: 2, 3, 5, and 7 are not considered to be truncatable primes.
   *
   * <p>See also <a href="https://projecteuler.net/problem=37">projecteuler.net problem 37</a>.
   */
  @Test
  public void shouldSolveProblem37() {
    assertThat(isTruncatablePrime(3797)).isTrue();
    List.of(2, 3, 5, 7).forEach(i -> assertThat(isTruncatablePrime(7)).isFalse());

    assertThat(sumOfTheElevenTruncatablePrimes()).isEqualTo(748_317);
  }
Ejemplo n.º 3
0
  /**
   * <strong>Problem 39 Integer right triangles</strong>
   *
   * <p>If <i>p</i> is the perimeter of a right angle triangle with integral length sides,
   * {<i>a,b,c</i>}, there are exactly three solutions for <i>p</i> = 120.
   *
   * <p>{20,48,52}, {24,45,51}, {30,40,50}
   *
   * <p>For which value of <i>p</i> ≤ 1000, is the number of solutions maximised?
   *
   * <p>See also <a href="https://projecteuler.net/problem=39">projecteuler.net problem 39</a>.
   */
  @Test
  public void shouldSolveProblem39() {
    assertThat(SOLUTIONS_FOR_PERIMETERS_UP_TO_1000.get(120))
        .isEqualTo(some(List.of(Tuple.of(20, 48, 52), Tuple.of(24, 45, 51), Tuple.of(30, 40, 50))));

    assertThat(perimeterUpTo1000WithMaximisedNumberOfSolutions()).isEqualTo(840);
  }
Ejemplo n.º 4
0
 private static boolean isTruncatablePrime(int prime) {
   return Match(prime)
       .of(
           Case(
               $(p -> p > 7),
               p -> {
                 final CharSeq primeSeq = CharSeq.of(Integer.toString(p));
                 return List.rangeClosed(1, primeSeq.length() - 1)
                     .flatMap(i -> List.of(primeSeq.drop(i), primeSeq.dropRight(i)))
                     .map(CharSeq::mkString)
                     .map(Long::valueOf)
                     .forAll(Utils.MEMOIZED_IS_PRIME::apply);
               }),
           Case($(), false));
 }
Ejemplo n.º 5
0
 @Test
 public void shouldConvertToSeq() {
   final Seq<?> actual = createIntTuple(1, 0, 0, 0, 0, 0, 0, 0).toSeq();
   assertThat(actual).isEqualTo(List.of(1, 0, 0, 0, 0, 0, 0, 0));
 }