class Numeric
def mirrornumber # 颠倒数(镜像数)这个是自己起的名字···其实本来想参考必修3做个回文数的但是找不到了就做了个这个···
if self.abs != self
raise ArgumentError.new("NaN")
end
num=0
sn=[]
x=0
self_array = self.to_s.scan(/./) # 分割每一位上的数字
for char in self_array # 获取位数
sn[x]=char.to_i
x+=1
end
for i in 0...sn.size
num+=sn[sn.size-1-i]*10**(sn.size-1-i)
end
return num.truncate == num ? num.truncate : num
end
def disnumber # 相反数
num=-self
return num.truncate == num ? num.truncate : num
end
def unnumber # 倒数
num=1.0/self
return num.truncate == num ? num.truncate : num
end
def prime(y) # 判定质数(y非0时必须用print,否则会p num => nil)
if self.abs != self
raise ArgumentError.new("NaN")
end
num=nil
if self==2
if y==0
num=1
else
num=self.to_s+"是质数"
end
end
if num==nil
i=2
r=self%i
until i>self-1 or r==0
r=self%i
i+=1
end
if y==0
if r==0
num=0
else
num=1
end
else
if r==0
num=self.to_s+"不是质数"
else
num=self.to_s+"是质数"
end
end
end
return num.to_s == num ? num.to_s : num
end
def zxgbs(y) # 标准的《九章算术》中先求最大公约数再求最小公倍数
num=nil
n=[self,y]
for i in 0...n.size
if n[i] <= 0
raise ArgumentError.new("NaN")
end
end
n=n.sort # 从小到大重新排序
if num==nil
i=n[0].cm2(n[1])
num=n[0]*n[1]/i
end
return num.to_s == num ? num.to_s : num
end
def cm1(y) # 辗转相除法求最大公约数(部分情况会失控)
n=[self,y]
for i in 0...n.size
if n[i] <= 0
raise ArgumentError.new("NaN")
end
end
n=n.sort # 从小到大重新排序
num=n[1]
i=num%n[0]
if i=0#余数为0时最大公约数为小的数
num=n[0]
else
while i!=0
num=n[0]
n[0]=i
i=num%n[0]
end
end
return num.to_s == num ? num.to_s : num
end
def cm2(y) # 更相损减术求最大公约数
n=[self,y]
for i in 0...n.size
if n[i] <= 0
raise ArgumentError.new("NaN")
end
end
i=1
if n[0]%2==0 and n[1]%2==0
n=[self/2,y/2]
i+=1
end
n=n.sort # 从小到大重新排序
num=n[1]-n[0]
if num==n[0] # 差等于减数时最大公约数为减数(差)
num=n[0]
else
while num!=n[0]
if n[0]>num
n=[num,n[0]]
else
n=[num,n[1]]
end
num=n[1]-n[0]
end
end
num*=i
return num.to_s == num ? num.to_s : num
end
# 一元N次N项式
def dxs1(y) # 逐项求
num=0
for i in 1..y.size
num+=y[i-1]*self**(y.size-i)
end
return num.truncate == num ? num.truncate : num
end
def dxs2(y) # 秦九韶算法
num=y[0]
for i in 1..y.size-1
num*=self
num+=y[i]
end
return num.truncate == num ? num.truncate : num
end
# 等差数列arithmetic_progression
def ari(x,y) # 等差数列求第y项
num=self+(y-1)*x
end
def arih1(x,y) # 等差数列已知首项self求前y项和
num=y*self+(y-1)*y*x/2
end
def arih2(x,y) # 等差数列已知末项self求前y项和
num=y*self-(y-1)*y*x/2
end
def arih3(x,y) # 等差数列已知首项x和末项y和元素数求和
num=self*(x+y)/2
end
# 等比数列geometric_sequence
def geo(x,y) # 等比数列求第y项
num=self*x**(y-1)
return num.truncate == num ? num.truncate : num
end
def geoh1(x,y) # 等比数列求前y项和
num=self # 获取a1
for i in 1...y
num+=x**(i) # 依次加上a2、a3...直到an
end
return num.truncate == num ? num.truncate : num
end
def geoh2(x,y) # 等比数列已知首项self求前y项和
num=(self*(1-x**y))/(1-x)
return num.truncate == num ? num.truncate : num
end
def geoh3(x,y) # 等比数列已知首项self和末项y求前y项和
num=(self-y*x)/(1-x)
return num.truncate == num ? num.truncate : num
end
def factorial # 阶乘
if self.abs != self
raise ArgumentError.new("NaN")
end
num=self
fnum=self
for i in 2...fnum # 乘数取1到阶乘基数本身之间的数
num*=i
end
return num.truncate == num ? num.truncate : num
end
def log(y) # 对数函数
if self.abs != self
raise ArgumentError.new("NaN")
end
x=self
num=1
while x!=y
x/=y
num+=1
end
return num.truncate == num ? num.truncate : num
end
def lg # 以10为底的对数函数
if self.abs != self
raise ArgumentError.new("NaN")
end
x=self
num=1
while x!=10
x/=10
num+=1
end
return num.truncate == num ? num.truncate : num
end
def sqr # 平方
return self ** 2
end
def cube # 立方
return self ** 3
end
def powten # 10 的 self 次方
return 10 ** self
end
def enf(y) # 任意次方
num = self ** y
return num.truncate == num ? num.truncate : num
end
def sqrt # 开平方
if self.abs != self
raise ArgumentError.new("NaN")
end
num = self ** 0.5
return num.truncate == num ? num.truncate : num
end
def cuberoot # 开立方
if self.abs != self
num = (self * (-1)) ** (1 / 3.0)
return num.truncate == num ? -num.truncate : -num
end
num = self ** (1 / 3.0)
return num.truncate == num ? num.truncate : num
end
def root(y) # 开任意次方
if self.abs != self and y % 2 == 0
raise ArgumentError.new("NaN")
end
if self.abs != self
num = (self * (-1)) ** (1.0 / y)
return num.truncate == num ? -num.truncate : -num
end
num = self ** (1.0 / y)
return num.truncate == num ? num.truncate : num
end
end
class Array
def average(need_dec = true) # 平均数 (是否使用浮点数)
if self.include?(nil)
ary = self.compact
else
ary = self
end
num = 0
if ary.size == 0
raise ZeroDivisionError.new("divided by 0")
end
for i in 0...ary.size
if ary[i].is_a?(Numeric)
num += ary[i]
else
raise TypeError.new("connot convert #{ary[i].class} into Numeric")
end
end
num /= ary.size.to_f
unless need_dec
return num.truncate
end
return num.truncate == num ? num.truncate : num
end
def variance(need_dec = true) # 方差
if self.include?(nil)
ary = self.compact
else
ary = self
end
num = 0
average=ary.average.to_f
for i in 0...ary.size
num+=((ary[i]-average)**2).to_f
end
num=(num/ary.size).to_f
end
def deviation(need_dec = true) # 标准差
if self.include?(nil)
ary = self.compact
else
ary = self
end
num = 0
num=((ary.variance.to_f)**0.5).to_f # 标准差是方差的算术平方根
end
def range(need_dec = true) # 极差(是否使用浮点数)
if self.include?(nil)
ary = self.compact
else
ary = self
end
num = 0
if ary.size == 0
raise ZeroDivisionError.new("divided by 0")
end
num = (ary.max-ary.min).to_f
unless need_dec
return num.truncate
end
return num.truncate == num ? num.truncate : num
end
def median(need_dec = true) # 中位数(是否使用浮点数)(需要从小到大排列)
if self.include?(nil)
ary = self.compact
else
ary = self
end
num = 0
ary=ary.sort # 从小到大重新排序
if ary.size == 0
raise ZeroDivisionError.new("divided by 0")
end
if ary.size%2==1
num = ary[ary.size/2].to_f
elsif ary.size%2==0
num = (ary[(ary.size-1)/2+1]+ary[(ary.size+1)/2-1])/2.to_f
end
unless need_dec
return num.truncate
end
return num.truncate == num ? num.truncate : num
end
def mode(need_dec = true) # 众数(是否使用浮点数)
if self.include?(nil)
ary = self.compact
else
ary = self
end
num = 0
if ary.size == 0
raise ZeroDivisionError.new("divided by 0")
end
modenum=[]
ary=ary.sort # 从小到大重新排序
for i in 0...ary.size # 计算每个数出现的次数
if ary[i].is_a?(Numeric)
if modenum[ary[i]]==nil
modenum[ary[i]]=1
else
modenum[ary[i]]+=1
end
else
raise TypeError.new("connot convert #{ary[i].class} into Numeric")
end
end
for i in 0...modenum.size # 填充数组nil元素
if modenum[i]==nil
modenum[i]=0
end
end
modemax=modenum.max # 找出最大的数
for i in 0...modenum.size # 找出最大的数的所在位置
if modemax==modenum[i]
num=i
end
end
unless need_dec
return num.truncate
end
return num.truncate == num ? num.truncate : num
end
end