DEV Community

Nicolas DUBIEN
Nicolas DUBIEN

Posted on • Edited on

2 1

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.

Sentry blog image

How to reduce TTFB

In the past few years in the web dev world, we’ve seen a significant push towards rendering our websites on the server. Doing so is better for SEO and performs better on low-powered devices, but one thing we had to sacrifice is TTFB.

In this article, we’ll see how we can identify what makes our TTFB high so we can fix it.

Read more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay