Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a Special Integer

I want to define a constant FOO in the namespace of Integer that is similar to Float::NAN in Float, which is itself an instance of Float. It will be used somewhat similar to symbols, i.e., to mark a special case (of an integer). I don't need it to be used in calculation, but I need it to have the following properties:

  • Its class must be Integer or a subclass of Integer, and it must behave so to methods related to class:

    Integer::FOO.kind_of?(Integer) # => true
    

    Optionally (if the class is Integer):

    Integer::FOO.class # => Integer
    Integer === Integer::FOO # => true
    Integer::FOO.instance_of?(Integer) # => true
    
  • It must be distinct from (ideally all) other integers:

    Integer::FOO == 0 # => false
    

    Ideally, I want it distinct from any other integer, but if that is not possible, I can live with a dirty hack that, say makes Integer::FOO be identical to the largest or the smallest integer, which are the least likely to hit any random given integer.

What is the best way to go about this?

like image 993
sawa Avatar asked May 28 '14 12:05

sawa


People also ask

What is a special integer?

An Integer x is called special in this case when, for every positive integer y such that y > x y \gt x y>x, x f ( x ) ≤ y f ( y ) \frac{x}{f(x)} \leq \frac{y}{f(y)} f(x)x≤f(y)y holds. Where, f ( d ) f(d) f(d) denotes the sum of the digits in the decimal representation on d.

How do you find special integers?

Steps to Find Special Number Find the factorial of all digits. Sum up the factorial and store it in a variable (s). Compare the sum with the given number (N). If the sum is equal to the number itself, the number (N) is a special number, else not.

What is a special number example?

What is Special Number? A number is said to be special number when the sum of factorial of its digits is equal to the number itself. Example- 145 is a Special Number as 1!+ 4!+

What is special number in C programming?

We call a number special number if all the digits in that number are the same. For example, 1, 11, 1111 are special numbers. We count the special numbers in order 1, 11, 111, 1111, 2, 22, 222, 2222, 3, 33, 333, 3333, and so on. We have to find out the total number of digits that are in special numbers up to k.


1 Answers

Ruby's metaprogramming methods make it easy to twist a generic object into the shape you desire:

class Integer
  FOO = Object.new
end

Integer::FOO.define_singleton_method(:kind_of?) do |klass|
  Integer.ancestors.include? klass
end

Integer::FOO.define_singleton_method(:class) do
  Integer
end

Integer::FOO.define_singleton_method(:instance_of?) do |klass|
  klass == Integer
end

oldteq = Integer.method(:===)

Integer.define_singleton_method(:===) do |obj|
  obj == Integer::FOO ? true : oldteq.call(obj)
end

Integer::FOO.kind_of? Integer
# true
Integer::FOO.class
# Integer
Integer === Integer::FOO
# true
Integer::FOO.instance_of? Integer
# true
Integer::FOO == 0
# false

The tricky part is making sure you cover all of the use cases. My code handles all of the requirements you listed but I have no idea what kind of weird side effects such a strange object would create.

like image 153
Max Avatar answered Sep 30 '22 11:09

Max