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
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
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
|
#<=>(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
|
#abs ⇒ Object
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
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
|
#hash ⇒ Object
909
910
911
|
# File 'lib/m500.rb', line 909
def hash
@a.hash
end
|
#inspect ⇒ Object
906
907
908
|
# File 'lib/m500.rb', line 906
def inspect
sprintf("Natural(%s)", @a)
end
|
#is_0? ⇒ 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
|
#next ⇒ Object
866
867
868
|
# File 'lib/m500.rb', line 866
def next
self + Natural(1)
end
|
#next_prime ⇒ Object
1037
1038
1039
1040
1041
1042
1043
|
# File 'lib/m500.rb', line 1037
def next_prime
a = Prep.new(self.to_i)
a.max = self.to_i
a.nextP(self.to_i)
end
|
#succ ⇒ Object
869
870
871
|
# File 'lib/m500.rb', line 869
def succ
self.next
end
|
#to_f ⇒ Object
894
895
896
|
# File 'lib/m500.rb', line 894
def to_f
@a.to_f
end
|
#to_i ⇒ Object
891
892
893
|
# File 'lib/m500.rb', line 891
def to_i
@a
end
|
#to_Q ⇒ Object
903
904
905
|
# File 'lib/m500.rb', line 903
def to_Q
Quotient(self, 1)
end
|
#to_s ⇒ Object
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_sgml ⇒ Object
779
780
781
|
# File 'lib/m500.rb', line 779
def to_sgml
"<mn #{sgml_id}class='natural'>#{@a}</mn>"
end
|