Skip to content
Query or verify happy and unhappy numbers
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
spec
src
.editorconfig
.gitignore
.security.txt
.travis.yml
CHANGELOG.md
LICENSE
README.md
shard.yml

README.md

Happy Number

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 in base-ten, and repeat the process until the number either equals 1 (where it will stay), or it loops endlessly in a cycle that does not include 1. Those numbers for which this process ends in 1 are happy numbers, while those that do not end in 1 are unhappy numbers (or sad numbers). --Wikipedia

This shard is a Crystal implementation of the happy number sequence.

Installation

  1. Add the dependency to your shard.yml:
dependencies:
  happy_number:
    github: grottopress/happy_number.cr
  1. Run shards install

Usage

require "happy_number"

# Check if a given number is happy
7_u8.happy? # => true

# Check if a given number is unhappy
1234_u16.unhappy? # => true
# Same as `1234_u16.sad?`

# Get all happy numbers in a given range
(1_u8..30_u8).happy # => [1, 7, 10, 13, 19, 23, 28]

# Get all unhappy numbers in a given range
(1_u8..15_u8).unhappy # => [2, 3, 4, 5, 6, 8, 9, 11, 12, 14, 15]

Security

Kindly report suspected security vulnerabilities in private, via contact details outlined in this repository's .security.txt file.

Contributing

  1. Fork it (https://github.com/grottopress/happy_number.cr/fork)
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

Contributors

You can’t perform that action at this time.