class Natural
Public Class Methods
new(a)
click to toggle source
# File lib/m500.rb, line 786 def initialize(a) if (a.kind_of?(Integer) && a >= 1) #(a.kind_of?(Fixnum) && a >= 1) or (a.kind_of?(Bignum) && a >= 1) @a = a else emptySet end end
new!(num)
click to toggle source
# File lib/m500.rb, line 782 def Natural.new!(num) new(num) end
Public Instance Methods
%(a)
click to toggle source
# File lib/m500.rb, line 854 def % (a) self.to_i.%(a.to_i) end
*(a)
click to toggle source
# File lib/m500.rb, line 821 def * (a) if a.kind_of?(Zahlen) or a.kind_of?(Counting) or a.kind_of?(Natural) or a.kind_of?(Integer) #a.kind_of?(Bignum) or a.kind_of?(Fixnum) Natural(@a * a.to_i) elsif a.kind_of?(NaughtClass) or a.kind_of?(EmptySetClass) naught elsif a.kind_of?(NANClass) or a.kind_of?(InfinityClass) infinity else x, y = a.coerce(self) Natural(x * y) end end
**(a)
click to toggle source
# File lib/m500.rb, line 843 def ** (a) if a.kind_of?(Zahlen) or a.kind_of?(Counting) or a.kind_of?(Natural) or a.kind_of?(Integer) #a.kind_of?(Bignum) or a.kind_of?(Fixnum) Natural(self.to_i ** a.to_i) else x, y = a.coerce(self) Natural(x ** y) end end
+(a)
click to toggle source
# File lib/m500.rb, line 794 def + (a) if a.kind_of?(Zahlen) or a.kind_of?(Counting) or a.kind_of?(Natural) or a.kind_of?(Integer)#a.kind_of?(Bignum) or a.kind_of?(Fixnum) Natural(self.to_i + a.to_i) elsif a.kind_of?(NaughtClass) or a.kind_of?(EmptySetClass) or a.kind_of?(NilClass) self elsif a.kind_of?(NANClass) or a.kind_of?(InfinityClass) infinity elsif infinity else x, y = a.coerce(self) Natural(x + y) end end
-(a)
click to toggle source
# File lib/m500.rb, line 808 def - (a) if a.kind_of?(Zahlen) or a.kind_of?(Counting) or a.kind_of?(Natural) or a.kind_of?(Integer) # a.kind_of?(Bignum) or a.kind_of?(Fixnum) z = @a.to_i - a.to_i z <= 0 ? emptySet : Natural(z) elsif a.kind_of?(NaughtClass) or a.kind_of?(EmptySetClass) self elsif a.kind_of?(NANClass) or a.kind_of?(InfinityClass) infinity else x, y = a.coerce(self) Natural(x - y) end end
/(a)
click to toggle source
# File lib/m500.rb, line 833 def / (a) if a.kind_of?(Zahlen) or a.kind_of?(Counting) or a.kind_of?(Natural) or a.kind_of?(Integer) #a.kind_of?(Bignum) or a.kind_of?(Fixnum) Natural(self.to_i / a.to_i) elsif a.kind_of?(NaughtClass) or a.kind_of?(EmptySetClass) infinity elsif a.kind_of?(NANClass) or a.kind_of?(InfinityClass) naught else end end
<=>(other)
click to toggle source
# File lib/m500.rb, line 872 def <=>(other) self.to_i <=> other.to_i end
==(other)
click to toggle source
# File lib/m500.rb, line 863 def == (other) @a == other end
abs()
click to toggle source
# File lib/m500.rb, line 860 def abs Natural(@a) end
coerce(other)
click to toggle source
# File lib/m500.rb, line 875 def coerce(other) if Integer === other #Fixnum === other or Bignum === other other <= 0 ? [emptySet,emptySet] : [other.to_N,self] elsif Natural === other or Counting === other or Zahlen === other other <= 0 ? [emptySet,emptySet] : [other,self.to_Z] elsif Quotient === other other <= 0 ? [emptySet,emptySet] : [other,self.to_Q] elsif Fraction === other other <= 0 ? [emptySet,emptySet] : [other,self.to_Frac] else other <= 0 ? [emptySet,emptySet] : [Float(other),@a.to_f] end end
div(a)
click to toggle source
# File lib/m500.rb, line 851 def div(a) self.to_i.div(a.to_i) end
divmod(a)
click to toggle source
Calls superclass method
# File lib/m500.rb, line 857 def divmod(a) super end
gcd(n)
click to toggle source
# File lib/m500.rb, line 909 def gcd(n) m = @a.to_i.abs n = n.to_i.abs return Naural(n) if m == 0 return Natural(m) if n == 0 b = 0 while n[0] == 0 && m[0] == 0 b += 1; n >>= 1; m >>= 1 end m >>= 1 while m[0] == 0 n >>= 1 while n[0] == 0 while m != n m, n = n, m if n > m m -= n; m >>= 1 while m[0] == 0 end # m << b Natural(m << b) end
gcd2(int)
click to toggle source
# File lib/m500.rb, line 927 def gcd2(int) a = @a.abs b = int.abs a, b = b, a if a < b while b != 0 void, a = a.divmod(b) a, b = b, a end return a end
gcdlcm(int)
click to toggle source
# File lib/m500.rb, line 943 def gcdlcm(int) a = @a.abs b = int.abs gcd = a.gcd(b) return gcd, (a.div(gcd)) * b end
hash()
click to toggle source
# File lib/m500.rb, line 906 def hash @a.hash end
inspect()
click to toggle source
# File lib/m500.rb, line 903 def inspect sprintf("Natural(%s)", @a) end
is_0?()
click to toggle source
# File lib/m500.rb, line 888 def is_0? @a === 0 ? true : false end
lcm(int)
click to toggle source
# File lib/m500.rb, line 937 def lcm(int) a = @a.abs b = int.abs gcd = a.gcd(b) (a.div(gcd)) * b end
next()
click to toggle source
# File lib/m500.rb, line 866 def next self + Natural(1) end
next_prime()
click to toggle source
# File lib/m500.rb, line 1034 def next_prime #require "time" a = Prep.new(self.to_i) a.max = self.to_i # Time.now a.nextP(self.to_i) end
succ()
click to toggle source
# File lib/m500.rb, line 869 def succ self.next end
to_Q()
click to toggle source
# File lib/m500.rb, line 900 def to_Q Quotient(self, 1) end
to_f()
click to toggle source
# File lib/m500.rb, line 894 def to_f @a.to_f end
to_i()
click to toggle source
# File lib/m500.rb, line 891 def to_i @a end
to_s()
click to toggle source
# File lib/m500.rb, line 897 def to_s "Natural(#{@a})" end
to_sgml()
click to toggle source
# File lib/m500.rb, line 779 def to_sgml "<mn #{sgml_id}class='natural'>#{@a}</mn>" end