Sha256: 7e1fae405ba7bc347d55087f4b225c09c7cc26cbe04bff34181614f8ee4a0687

Contents?: true

Size: 491 Bytes

Versions: 36

Compression:

Stored size: 491 Bytes

Contents

# coding: utf-8

class Integer

# A Carol number is an integer of the form <i>4<sup>n</sup> − 2<sup>n +
# 1</sup> − 1</i>.
#
# This method returns true if self is a Carol number; false otherwise.
#
#     −1.carol?  #=> true 
#     959.carol? #=> true 
#     32.carol?  #=> false
#
  def carol?
    return true if self == 7
    return true if self == -1
    a, b = to_s(2).match(/^(1+)0(1+)$/).to_a[1..-1]
    return false if (a.nil? or b.nil?)
    b.length == (a.length + 3)
  end
end

Version data entries

36 entries across 36 versions & 1 rubygems

Version Path
numb-0.186.0 lib/numb/carol.rb
numb-0.185.0 lib/numb/carol.rb
numb-0.184.0 lib/numb/carol.rb
numb-0.181.0 lib/numb/carol.rb
numb-0.170.0 lib/numb/carol.rb
numb-0.152.0 lib/numb/carol.rb
numb-0.138.0 lib/numb/carol.rb
numb-0.125.0 lib/numb/carol.rb
numb-0.114.0 lib/numb/carol.rb
numb-0.111.0 lib/numb/carol.rb
numb-0.109.0 lib/numb/carol.rb
numb-0.99.0 lib/numb/carol.rb
numb-0.96.0 lib/numb/carol.rb
numb-0.89.0 lib/numb/carol.rb
numb-0.84.0 lib/numb/carol.rb
numb-0.77.0 lib/numb/carol.rb
numb-0.72.1 lib/numb/carol.rb
numb-0.72.0 lib/numb/carol.rb
numb-0.68.0 lib/numb/carol.rb
numb-0.63.0 lib/numb/carol.rb