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



709
710
711
# File 'lib/m500.rb', line 709

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

Instance Method Details

#%(a) ⇒ Object



784
785
786
# File 'lib/m500.rb', line 784

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

#*(a) ⇒ Object



751
752
753
754
755
756
757
758
759
760
761
762
# File 'lib/m500.rb', line 751

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



773
774
775
776
777
778
779
780
# File 'lib/m500.rb', line 773

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



721
722
723
724
725
726
727
728
729
730
731
732
733
734
# File 'lib/m500.rb', line 721

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



735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
# File 'lib/m500.rb', line 735

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
p z
p z<=0
    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



763
764
765
766
767
768
769
770
771
772
# File 'lib/m500.rb', line 763

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



802
803
804
# File 'lib/m500.rb', line 802

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

#==(other) ⇒ Object



793
794
795
# File 'lib/m500.rb', line 793

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

#absObject



790
791
792
# File 'lib/m500.rb', line 790

def abs
 Natural(@a)
end

#coerce(other) ⇒ Object



805
806
807
808
809
810
811
812
813
814
815
816
817
# File 'lib/m500.rb', line 805

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



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

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

#divmod(a) ⇒ Object



787
788
789
# File 'lib/m500.rb', line 787

def divmod(a)
  super
end

#gcd(n) ⇒ Object



842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
# File 'lib/m500.rb', line 842

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



860
861
862
863
864
865
866
867
868
869
# File 'lib/m500.rb', line 860

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



876
877
878
879
880
881
# File 'lib/m500.rb', line 876

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

#hashObject



839
840
841
# File 'lib/m500.rb', line 839

def hash
  @a.hash
end

#inspectObject



836
837
838
# File 'lib/m500.rb', line 836

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

#is_0?Boolean

Returns:

  • (Boolean)


818
819
820
# File 'lib/m500.rb', line 818

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

#lcm(int) ⇒ Object



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

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

#nextObject



796
797
798
# File 'lib/m500.rb', line 796

def next
  self + Natural(1)
end

#next_primeObject



967
968
969
970
971
972
973
# File 'lib/m500.rb', line 967

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



799
800
801
# File 'lib/m500.rb', line 799

def succ
  self.next
end

#to_fObject



824
825
826
# File 'lib/m500.rb', line 824

def to_f
  @a.to_f
end

#to_iObject



821
822
823
# File 'lib/m500.rb', line 821

def to_i
  @a
end

#to_QObject



833
834
835
# File 'lib/m500.rb', line 833

def to_Q
  Quotient(self, 1)
end

#to_sObject



830
831
832
# File 'lib/m500.rb', line 830

def to_s
  @a.to_s
end

#to_s!Object



827
828
829
# File 'lib/m500.rb', line 827

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

#to_sgmlObject



706
707
708
# File 'lib/m500.rb', line 706

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