2023-06-01

 01 Jun 2023

Return Perfect Number

Taken from DailyCodingProblem

A number is considered perfect if its digits sum to exactly 10.

Given a positive integer n, return the n-th perfect number.

For exmaple, given 1, you should return 19. Given 2, you should return 28.

Solution

This is the answer I came up with:

function perfect(n: number): number {
  let digits = [n, 0]
  while (digits[0] + digits[1] !== 10) {
    digits[1]++
  }
  return Number(digits.join(''))
}

Testing

Solution tested in REPL:

https://www.typescriptlang.org/play?#code/GYVwdgxgLglg9mABABwKYCdiugCjALkTBAFsAjDASkOPI0QG8AoRRAG1SkQBMYBzGFADOiALyIA2mAA0iAAwBdFogDuACxgdEOXgOETFiANQ9+goRICMCxAEJR4y3MqNlrXeasKjR5QF9ldE4QdCQAOVIKdB0zYQA6ACs4GDAcAHI0ykomAKYIBCE4Dji2OD4cZTRMbCgcS0pZAHpGxEsATkqMLFwAJgbEZsQegA5O6twAVhdBiYmmSiA

Copyright © Paramdeo Singh · All Rights Reserved · Built with Jekyll

This node last updated October 9, 2024 and is permanently morphing...

Paramdeo Singh Guyana

Generalist. Edgerunner. Riding the wave of consciousness in this treacherous mortal sea.

Technology Design Strategy Literature Personal Blogs
Search Site

Results are from Blog, Link Dumps, and #99Problems