DEV Community

DPC
DPC

Posted on

1 1 1

Daily JavaScript Challenge #JS-105: Check if a Number is a Happy Number

Daily JavaScript Challenge: Check if a Number is a Happy Number

Hey fellow developers! 👋 Welcome to today's JavaScript coding challenge. Let's keep those programming skills sharp!

The Challenge

Difficulty: Medium

Topic: Mathematics

Description

A happy number is a number which eventually reaches 1 when replaced repeatedly by the sum of the square of its digits. If it loops endlessly in a cycle, then it is not a happy number. For example, starting with 19, the sequence is: 1² + 9² = 82, 8² + 2² = 68, 6² + 8² = 100, 1² + 0² + 0² = 1. Hence, 19 is a happy number. Implement a function to check if a given number is a happy number.

Ready to Begin?

https://www.dpcdev.com/

  1. Fork this challenge
  2. Write your solution
  3. Test it against the provided test cases
  4. Share your approach in the comments below!

Want to Learn More?

Check out the documentation about this topic here: https://en.wikipedia.org/wiki/Happy_number

Join the Discussion!

  • How did you approach this problem?
  • Did you find any interesting edge cases?
  • What was your biggest learning from this challenge?

Let's learn together! Drop your thoughts and questions in the comments below. 👇


This is part of our Daily JavaScript Challenge series. Follow me for daily programming challenges and let's grow together! 🚀

javascript #programming #coding #dailycodingchallenge #webdev

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

Top comments (1)

Collapse
 
jamescurran profile image
James Curran •

In C#:
This preserves all the info it learns determining if a number is happy, so future searches are faster.

HashSet<int> HappyNums = new HashSet<int>();
HashSet<int> NonHappyNums = new HashSet<int>();

bool IsHappyNumber(int num)
{
    var foundNums = new HashSet<int>();
    while (num != 1)
    {
        if (HappyNums.Contains(num))
            break;

        if(NonHappyNums.Contains(num)|| foundNums.Contains(num))
        {
            AddRange(NonHappyNums, foundNums);
            return false;
        }
        foundNums.Add(num);
        num = SumDigitsSquared(num);
    }

    foundNums.Add(num);
    AddRange(HappyNums, foundNums);
    return true;
}

int SumDigitsSquared(int num) => Digits(num).Select(n => n * n).Sum();


IEnumerable<int> Digits(int n)
{
    while (n > 0)
    {
        yield return n % 10;
        n = n /10;
    }
}

void AddRange(HashSet<int> hs, IEnumerable<int> others)
{
    foreach (int n in others)
        hs.Add(n);
}
Enter fullscreen mode Exit fullscreen mode

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs