Class: Natural

Inherits:
Numeric show all
Includes:
SGML
Defined in:
lib/m500.rb

Direct Known Subclasses

Counting

Defined Under Namespace

Classes: Prep

Class Method Summary collapse

Instance Method Summary collapse

Methods included from SGML

#sgml_id, #tog_sgml_id

Methods inherited from Numeric

#irrational?, #rational?, #sgml_id, #to_Dec, #to_Frac, #to_K, #to_N, #to_N0, #to_R, #to_Sig, #to_Z

Class Method Details

.new!(num) ⇒ Object



782
783
784
# File 'lib/m500.rb', line 782

def Natural.new!(num)
  new(num)
end

Instance Method Details

#%(a) ⇒ Object



854
855
856
# File 'lib/m500.rb', line 854

def % (a)
  self.to_i.%(a.to_i)
end

#*(a) ⇒ Object



821
822
823
824
825
826
827
828
829
830
831
832
# 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?(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) ⇒ Object



843
844
845
846
847
848
849
850
# 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?(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) ⇒ Object



794
795
796
797
798
799
800
801
802
803
804
805
806
807
# 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?(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) ⇒ Object



808
809
810
811
812
813
814
815
816
817
818
819
820
# 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?(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) ⇒ Object



833
834
835
836
837
838
839
840
841
842
# 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?(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) ⇒ Object



872
873
874
# File 'lib/m500.rb', line 872

def <=>(other)
  self.to_i <=> other.to_i
end

#==(other) ⇒ Object



863
864
865
# File 'lib/m500.rb', line 863

def == (other)
  @a == other
end

#absObject



860
861
862
# File 'lib/m500.rb', line 860

def abs
 Natural(@a)
end

#coerce(other) ⇒ Object



875
876
877
878
879
880
881
882
883
884
885
886
887
# File 'lib/m500.rb', line 875

def coerce(other)
  if 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) ⇒ Object



851
852
853
# File 'lib/m500.rb', line 851

def div(a)
  self.to_i.div(a.to_i)
end

#divmod(a) ⇒ Object



857
858
859
# File 'lib/m500.rb', line 857

def divmod(a)
  super
end

#gcd(n) ⇒ Object



912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
# File 'lib/m500.rb', line 912

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) ⇒ Object



930
931
932
933
934
935
936
937
938
939
# File 'lib/m500.rb', line 930

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) ⇒ Object



946
947
948
949
950
951
# File 'lib/m500.rb', line 946

def gcdlcm(int)
  a = @a.abs
  b = int.abs
  gcd = a.gcd(b)
  return gcd, (a.div(gcd)) * b
end

#hashObject



909
910
911
# File 'lib/m500.rb', line 909

def hash
  @a.hash
end

#inspectObject



906
907
908
# File 'lib/m500.rb', line 906

def inspect
  sprintf("Natural(%s)", @a)
end

#is_0?Boolean

Returns:

  • (Boolean)


888
889
890
# File 'lib/m500.rb', line 888

def is_0?
  @a === 0 ? true : false
end

#lcm(int) ⇒ Object



940
941
942
943
944
945
# File 'lib/m500.rb', line 940

def lcm(int)
  a = @a.abs
  b = int.abs
  gcd = a.gcd(b)
  (a.div(gcd)) * b
end

#nextObject



866
867
868
# File 'lib/m500.rb', line 866

def next
  self + Natural(1)
end

#next_primeObject



1037
1038
1039
1040
1041
1042
1043
# File 'lib/m500.rb', line 1037

def next_prime
  #require "time"
  a = Prep.new(self.to_i)
  a.max = self.to_i
  # Time.now
  a.nextP(self.to_i)
end

#succObject



869
870
871
# File 'lib/m500.rb', line 869

def succ
  self.next
end

#to_fObject



894
895
896
# File 'lib/m500.rb', line 894

def to_f
  @a.to_f
end

#to_iObject



891
892
893
# File 'lib/m500.rb', line 891

def to_i
  @a
end

#to_QObject



903
904
905
# File 'lib/m500.rb', line 903

def to_Q
  Quotient(self, 1)
end

#to_sObject



900
901
902
# File 'lib/m500.rb', line 900

def to_s
  @a.to_s
end

#to_s!Object



897
898
899
# File 'lib/m500.rb', line 897

def to_s!
  "Natural(#{@a})"
end

#to_sgmlObject



779
780
781
# File 'lib/m500.rb', line 779

def to_sgml
  "<mn #{sgml_id}class='natural'>#{@a}</mn>"
end