From 91d31de2354f617812d8793665f89c2d91fccf37 Mon Sep 17 00:00:00 2001 From: Min Huang Date: Thu, 9 Jan 2025 16:46:03 -0800 Subject: [PATCH] solves52 --- src/52.rkt | 24 ------------------------ src/52.ts | 23 +++++++++++++++++++++++ test/52.test.ts | 7 +++++++ 3 files changed, 30 insertions(+), 24 deletions(-) delete mode 100644 src/52.rkt create mode 100644 src/52.ts create mode 100644 test/52.test.ts diff --git a/src/52.rkt b/src/52.rkt deleted file mode 100644 index a1eaa663..00000000 --- a/src/52.rkt +++ /dev/null @@ -1,24 +0,0 @@ -#lang racket -(require rackunit) -(require srfi/41) -(require "lib/core.rkt") - - -(define (same-digits? a b . more) - (let* ((xs (append (list a) (list b) more)) - (ys (map integer->list xs))) - (andmap (curry set=? (car ys)) (cdr ys)))) - -(define (products n) - (map (curry * n) (range 1 (add1 6)))) - -(define xs - (stream-map products (stream-from 1))) - -(define ys - (stream-drop-while (lambda (n) (not (apply same-digits? n))) xs)) - -(define result (car (stream-car ys))) - -(displayln result) -(check-equal? result 142857) diff --git a/src/52.ts b/src/52.ts new file mode 100644 index 00000000..8f6846f9 --- /dev/null +++ b/src/52.ts @@ -0,0 +1,23 @@ +// Computes the smallest positive integer x such that 2x, 3x, 4x, 5x, and 6x, contain the same digits. +// +// See {@link https://projecteuler.net/problem=52} +export default function compute() { + let x = 1; + + while (true) { + const s = sort(x); + const isValid = [2, 3, 4, 5, 6].every(m => { + return sort(x * m) === s; + }); + + if (isValid) { + return x; + } + + x++; + } +} + +function sort(x: number): string { + return x.toString().split('').sort().join(''); +} diff --git a/test/52.test.ts b/test/52.test.ts new file mode 100644 index 00000000..a0b23a16 --- /dev/null +++ b/test/52.test.ts @@ -0,0 +1,7 @@ +import compute from '../src/52'; + +describe('permuted multiples', () => { + test('problem 52', async () => { + expect(compute()).toBe(142857); + }); +});