Class: Natural
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
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
|
#<=>(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
|
#abs ⇒ Object
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
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
|
#hash ⇒ Object
839
840
841
|
# File 'lib/m500.rb', line 839
def hash
@a.hash
end
|
#inspect ⇒ Object
836
837
838
|
# File 'lib/m500.rb', line 836
def inspect
sprintf("Natural(%s)", @a)
end
|
#is_0? ⇒ 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
|
#next ⇒ Object
796
797
798
|
# File 'lib/m500.rb', line 796
def next
self + Natural(1)
end
|
#next_prime ⇒ Object
967
968
969
970
971
972
973
|
# File 'lib/m500.rb', line 967
def next_prime
a = Prep.new(self.to_i)
a.max = self.to_i
a.nextP(self.to_i)
end
|
#succ ⇒ Object
799
800
801
|
# File 'lib/m500.rb', line 799
def succ
self.next
end
|
#to_f ⇒ Object
824
825
826
|
# File 'lib/m500.rb', line 824
def to_f
@a.to_f
end
|
#to_i ⇒ Object
821
822
823
|
# File 'lib/m500.rb', line 821
def to_i
@a
end
|
#to_Q ⇒ Object
833
834
835
|
# File 'lib/m500.rb', line 833
def to_Q
Quotient(self, 1)
end
|
#to_s ⇒ Object
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_sgml ⇒ Object
706
707
708
|
# File 'lib/m500.rb', line 706
def to_sgml
"<mn #{sgml_id}class='natural'>#{@a}</mn>"
end
|