Skip to content
    SeaCode
    Learning tracksCode ArenaWorkshops & EventsInstructors
    Log inSign up
    Practice problems / Happy number

    Happy number

    Easymathhash setfast and slow pointers

    Replace a number with the sum of the squares of its digits, and repeat. If it reaches 1 the number is happy; if it loops forever without reaching 1 it isn't. Return true if n is happy.

    Example 1

    Input:  n = 19
    Output: true

    1² + 9² = 82 → 68 → 100 → 1.

    Example 2

    Input:  n = 2
    Output: false

    Constraints

    • 1 ≤ n ≤ 2³¹ - 1

    Submit checks your solution against 8 tests, including hidden ones.

    /**
     * @param {int} n
     * @return {bool}
     */
    function isHappy(n) {
      
    }
    
    Sign in to run
    SeaCode

    Learn to code for real, online or at our branch in front of Cairo University, Giza.

    Learn

    • All courses
    • Interactive tutorials
    • Interview practice problems
    • Mock interview
    • Learning tracks
    • Code Arena
    • Workshops & Events
    • Knowledge library
    • Team projects

    Your account

    • Log in
    • Sign up
    • Learning plan
    • Certificates
    • Site map

    Contact us

    • WhatsApp
    • 010 9094 1104
    • In front of Cairo University, Giza

    © 2026 SeaCode. All rights reserved.