Created
July 30, 2015 21:51
-
-
Save tshddx/a6db8f1e52f1455a5b2a to your computer and use it in GitHub Desktop.
Preferred number generator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# https://en.wikipedia.org/wiki/Preferred_number | |
def preferred_numbers(x, precision=2) | |
root = 10 ** (1 / x.to_f) | |
return x.times.map {|i| (root ** i).round(precision)} | |
end | |
<<EXAMPLES | |
> preferred_numbers(2) | |
=> [1.0, 3.16] | |
> preferred_numbers(3) | |
=> [1.0, 2.15, 4.64] | |
> preferred_numbers(5) | |
=> [1.0, 1.58, 2.51, 3.98, 6.31] | |
> preferred_numbers(10) | |
=> [1.0, 1.26, 1.58, 2.0, 2.51, 3.16, 3.98, 5.01, 6.31, 7.94] | |
> preferred_numbers(3, 0) | |
=> [1, 2, 5] | |
# Like Euro denominations! | |
EXAMPLES |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment