Advent of PBT 2021 - Day 17 - Solution

dubzzz

Nicolas DUBIEN

Posted on December 17, 2021

Advent of PBT 2021 - Day 17 - Solution

Our algorithm was: isHumbleNumber.
Go to the subject itself for more details

CodeSandbox with a possible set of properties you may have come with: https://codesandbox.io/s/advent-of-pbt-day-17-solution-75tuu?file=/src/index.spec.ts&previewwindow=tests


Property 1: should consider any composite of primes <=7 as humble

for any number n product of factors <=7
it should consider n as an humble number

Written with fast-check:

it("should consider any composite of primes <=7 as humble", () => {
  fc.assert(
    fc.property(
      fc.array(fc.integer({ min: 2, max: 7 }), { minLength: 1 }),
      (factors) => {
        // Arrange
        let n = 1;
        for (const f of factors) {
          if (n * f > 2 ** 31 - 1) break;
          n = n * f;
        }

        // Act / Assert
        expect(isHumbleNumber(n)).toBe(true);
      }
    )
  );
});
Enter fullscreen mode Exit fullscreen mode

Property 2: should consider any composite with one prime factor >7 as non-humble

for any number n with at least one factor not divisible by any number in [2, 7]
it should consider n as a non-humble number

Written with fast-check:

it("should consider any composite with one prime factor >7 as non-humble", () => {
  fc.assert(
    fc.property(
      fc
        .integer({ min: 11 }) // 8,9,10 would be filtered
        .filter((v) => v % 2 !== 0)
        .filter((v) => v % 3 !== 0)
        .filter((v) => v % 5 !== 0)
        .filter((v) => v % 7 !== 0),
      fc.array(fc.integer({ min: 1, max: 195225786 })),
      (tooLarge, factors) => {
        // Arrange
        let n = tooLarge;
        for (const f of factors) {
          if (n * f > 2 ** 31 - 1) break;
          n = n * f;
        }

        // Act / Assert
        expect(isHumbleNumber(n)).toBe(false);
      }
    )
  );
});
Enter fullscreen mode Exit fullscreen mode

Back to "Advent of PBT 2021" to see topics covered during the other days and their solutions.

More about this serie on @ndubien or with the hashtag #AdventOfPBT.

💖 💪 🙅 🚩
dubzzz
Nicolas DUBIEN

Posted on December 17, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Advent of PBT 2021 - Day 24
challenge Advent of PBT 2021 - Day 24

December 24, 2021

Advent of PBT 2021 - Day 24 - Solution
challenge Advent of PBT 2021 - Day 24 - Solution

December 24, 2021

Advent of PBT 2021 - Day 23
challenge Advent of PBT 2021 - Day 23

December 23, 2021

Advent of PBT 2021 - Day 23 - Solution
challenge Advent of PBT 2021 - Day 23 - Solution

December 23, 2021

Advent of PBT 2021 - Day 22
challenge Advent of PBT 2021 - Day 22

December 22, 2021