Giter VIP home page Giter VIP logo

happy's Introduction

happy

Playing around with some stuff.

Code to calculate happy numbers

Approach

Given a number n, we generate a sequence by taking the sum of the squares of its digits to get n1. Then we do the same to n1 to get n2, and so on. The number n is happy if this process eventually converges to 1.

The approach we take is quite simple. We can show that this algorithm applied to any number either converges to 1 or the "non-convergent" sequence

4, 16, 37, 58, 89, 145, 42, 20, 4,...

Let a number n have d digits. The maximum possible sum of squares of digits is 9^2 times the number of digits, which is 81d. Then, with d >= 4, we have

n >= 10^(d-1) > 81d

So any number bigger than 999 gets smaller. The number from 1 to 999 with the largest sum of squared digits is 999, whose sum of squared digits is 243. So all numbers between 244 and 999 become less than 244. The number from 1 to 243 with the largest sum of squared digits is 199, whose sum of squared digits is 163. The number under 163 with largest sum of squared digits is 159, which becomes 107. Then, number with the largest sum of squared digits less than or equal to 107 is 107, which becomes 50.

So all numbers greater than 99 become a number less than or equal to 99 in this process. Then, the code in happy_experiment.py verifies that all of these numbers from 1 to 99 either ends in 1 or the sequence described above.

Performance

The basic way to compute whether or not a number is happy is to apply the algorithm and at every step check if the number is 1 or 4. I found that by checking if the number is any of the elements in the "non-convergent" sequence, we could speed up the process a little. I tested a binary search approach, but because the list of unique elements in the non-convergent sequence is short, this had worse approach because function calls are expensive in Python.

To quickly compute squares, I created a dictionary that mapped the string form of a digit 0-9 to its square (as an int).

Files

  • happy.py: The main code that contains the function that checks whether or not a number is happy, as well as driver code to check which of the numbers from 1 to 10000 is happy.

  • happy_experiment.py: Verifies that all numbers between 1 and 99 inclusive end in the "non-convergent" sequence given above.

  • test_happy.py: Contains unit tests.

References

https://en.wikipedia.org/wiki/Happy_number

happy's People

Stargazers

Jeffrey Kimmel avatar

Watchers

James Cloos avatar Andy Shi avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.