From 69e937d98e5c06a42ec1be1bb6b656919ce4e839 Mon Sep 17 00:00:00 2001 From: Loosetooth Date: Sun, 2 Feb 2025 22:48:09 +0100 Subject: [PATCH] add kata --- 5kyu/buddy-pairs/description.md | 27 +++++++++++++++++++++++++++ 5kyu/buddy-pairs/solution.ts | 33 +++++++++++++++++++++++++++++++++ 5kyu/buddy-pairs/tests.ts | 19 +++++++++++++++++++ 3 files changed, 79 insertions(+) create mode 100644 5kyu/buddy-pairs/description.md create mode 100644 5kyu/buddy-pairs/solution.ts create mode 100644 5kyu/buddy-pairs/tests.ts diff --git a/5kyu/buddy-pairs/description.md b/5kyu/buddy-pairs/description.md new file mode 100644 index 0000000..5300a25 --- /dev/null +++ b/5kyu/buddy-pairs/description.md @@ -0,0 +1,27 @@ +Buddy pairs +You know what divisors of a number are. The divisors of a positive integer n are said to be proper when you consider only the divisors other than n itself. In the following description, divisors will mean proper divisors. For example for 100 they are 1, 2, 4, 5, 10, 20, 25, and 50. + +Let s(n) be the sum of these proper divisors of n. Call buddy two positive integers such that the sum of the proper divisors of each number is one more than the other number: + +(n, m) are a pair of buddy if s(m) = n + 1 and s(n) = m + 1 + +For example 48 & 75 is such a pair: + +Divisors of 48 are: 1, 2, 3, 4, 6, 8, 12, 16, 24 --> sum: 76 = 75 + 1 +Divisors of 75 are: 1, 3, 5, 15, 25 --> sum: 49 = 48 + 1 +Task +Given two positive integers start and limit, the function buddy(start, limit) should return the first pair (n m) of buddy pairs such that n (positive integer) is between start (inclusive) and limit (inclusive); m can be greater than limit and has to be greater than n + +If there is no buddy pair satisfying the conditions, then return "Nothing" or (for Go lang) nil or (for Dart) null; (for Lua, Pascal, Perl, D) [-1, -1]; (for Erlang {-1, -1}). + +Examples +(depending on the languages) + +buddy(10, 50) returns [48, 75] +buddy(48, 50) returns [48, 75] +or +buddy(10, 50) returns "(48 75)" +buddy(48, 50) returns "(48 75)" +Notes +for C: The returned string will be free'd. +See more examples in "Sample Tests:" of your language. \ No newline at end of file diff --git a/5kyu/buddy-pairs/solution.ts b/5kyu/buddy-pairs/solution.ts new file mode 100644 index 0000000..82c3e61 --- /dev/null +++ b/5kyu/buddy-pairs/solution.ts @@ -0,0 +1,33 @@ + +const properDivisors = (n: number): number[] => { + const divs: number[] = []; + for (let i = 1; i <= Math.sqrt(n); i++) { + if (n % i === 0) { + divs.push(i); + const otherDivisor = n / i; + if (otherDivisor !== i && otherDivisor !== n) { + divs.push(otherDivisor); + } + } + } + + return divs; +} + +const sumOfDivisors = (n: number): number => { + const divs = properDivisors(n) + return divs.reduce((acc, div) => acc + div, 0); +} + +export function buddy(start: number, limit: number): number[] { + for (let n = start; n <= limit; n++) { + const sumN = sumOfDivisors(n); + const m = sumN - 1; + const sumM = sumOfDivisors(m); + if (m > n && sumM === n + 1) { + return [n, m]; + } + } + + return []; +} \ No newline at end of file diff --git a/5kyu/buddy-pairs/tests.ts b/5kyu/buddy-pairs/tests.ts new file mode 100644 index 0000000..02c8701 --- /dev/null +++ b/5kyu/buddy-pairs/tests.ts @@ -0,0 +1,19 @@ +import { assert } from "chai"; +import { buddy } from "./solution"; + + +describe("buddy", function() { + + function testing(start: number, limit: number, expect:number[]) { + let actual = buddy(start, limit); + assert.deepEqual(actual, expect); + } + + it("Basic tests", function() { + testing(10, 50, [48, 75] ); + testing(1071625, 1103735, [1081184, 1331967] ); + testing(57345, 90061, [62744, 75495] ); + testing(2382, 3679, [] ); + + }) +});