Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8625351
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T07:45:49+00:00 2026-06-12T07:45:49+00:00

A happy number is defined by the following process. Starting with any positive integer,

  • 0

A happy number is defined by the following process. Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until the number equals 1.

But when number is not a happy number it loops endlessly in a cycle which does not include 1.

i have coded happy number problem in python but the problem is when a number is not happy , then how could i stop the iterating cycle. since it will not end with 1 and will keep on repeating itself.

def happynumber(number):

while(number!=1):
    numberstr = str(number) #converting a number to string
    index=0 
    sum=0
    while(index!=len(numberstr)):
        sum = sum + int(numberstr[index])*int(numberstr[index])
        index = index+1
    print sum

    number = sum
return number
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T07:45:50+00:00Added an answer on June 12, 2026 at 7:45 am

    You can detect unhappy numbers with a constant amount of memory. According to Wikipedia, for any positive integer starting point, the sequence will terminate at one, or loop forever at 4, 16, 37, 58, 89, 145, 42, 20, 4. Since no other loops exist, it is easy to test for unhappiness.

    def isHappy(x):
        while True:
            if x == 1:
                return True
            if x == 4:
                return False
            x = nextNumberInSequence(x)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would be happy to access any element of multi-dimensional hash-array by a shorter
Is there any logic in setting the number of maximum number of worker processes
I'm a happy user of Github. Over time I have accrued a number of
Currently, our DBAs require the TFS changeset number for any scripts/stored procedures/functions that need
a = int(raw_input(Enter a number to check if it is happy:)) l = map(int,
I was a happy customer of Google Analytics starting from the Urchin times. But
Any recommendations for a high-quality compact (no number pad) keyboard, preferably $50 or less?
Happy New Year SO users! I have an issue that I hope someone can
Happy easters, everyone. I am currently learning topological sort and having a question about
I'm happy with the php code I'm currently using to display the file contents

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.