I wonder if there is anything I can post to Qiita. I dug up the script when I tried Project Euler more than 6 years ago, so Write it
The execution environment is as follows. ruby 2.6.3p62 macOS Catalina 10.15.6 zsh
:zero::one::two::three::four::five::six::seven::eight::nine::ten:
Please refer to the following for those who are divisors. https://ja.wikipedia.org/wiki/約数
divisor.rb
def create_divisor (num)
    divisor_ary = Array.new
    partner_divisor_ary = Array.new
    
    if num < 1 then
        return nil
    elsif num == 1 then
        divisor_ary.push 1
    else
        i = 1
        partner_divisor = 0
        until i == partner_divisor do
            if num % i == 0 then
                divisor_ary.push i
                partner_divisor = num / i
                if partner_divisor != i then
                    partner_divisor_ary.unshift partner_divisor
                else
                    break
                end
            end
            i += 1
        end
        divisor_ary += partner_divisor_ary
    end
    
    return divisor_ary
end
class Integer
	def divisor
		return create_divisor(self)
	end
end
p 8.divisor
p 128.divisor
p 12345.divisor
The execution result is as follows.
% ruby divisor.rb
[1, 2, 4, 8]
[1, 2, 4, 8, 16, 32, 64, 128]
[1, 3, 5, 15, 823, 2469, 4115, 12345]
Amicable numbers ... I think it's a good name with a lot of rear. As natural numbers a and b ・ Sum of divisors of a -a = b And, ・ Sum of divisors of b-b = a When it comes to, it seems that a and b are called amicable numbers. See below for a detailed and accurate explanation. https://ja.wikipedia.org/wiki/友愛数
number_amicable.rb
class Integer
	def divisor
		return create_divisor(self)
	end
    def amicable
        amicable_number = nil
        
        if self < 1 then
            return amicable_number
        end
        
        divisor_ary = self.divisor
        divisor_ary.pop
        unless divisor_ary.empty? then
            partner_number = divisor_ary.inject(:+)
            if partner_number != self then
                partner_divisor_ary = partner_number.divisor
                partner_divisor_ary.pop
                if partner_divisor_ary.inject(:+) == self then
                    amicable_number = partner_number
                end
            end
        end
        return amicable_number
    end
end
p 220.amicable
p 284.amicable
p 17296.amicable
p 18416.amicable
p 200.amicable
The execution result is as follows.
% ruby number_amicable.rb
284
220
18416
17296
nil
As a natural number a (1) If the sum of divisors = a * 2, then a is a perfect number. (2) If the sum of divisors> a * 2, then a is an abundant number. (3) If the sum of divisors <a * 2, then a is the defendicient number. it seems like. See below for a detailed and accurate explanation. https://ja.wikipedia.org/wiki/完全数 https://ja.wikipedia.org/wiki/過剰数 https://ja.wikipedia.org/wiki/不足数
number_p_a_d.rb
class Integer
	def divisor
		return create_divisor(self)
	end
	def compare_divisor_total
		sum_divisor = self.divisor.inject(:+)
		sum_divisor -= self
		if sum_divisor > self then
			return "abundant number"
		elsif sum_divisor == self then
			return "perfect number"
		else
			return "deficient number"
		end
	end
end
p 496.compare_divisor_total
p 20.compare_divisor_total
p 15.compare_divisor_total
p 1.compare_divisor_total
The execution result is as follows.
% ruby number_p_a_d.rb
"perfect number"
"abundant number"
"deficient number"
"deficient number"
The number of palindromes is the same number whether read from the top or the bottom. See below for a detailed and accurate explanation. https://ja.wikipedia.org/wiki/回文数
number_palindrome.rb
class Integer
	def palindrome?
		str = self.to_s
		if str[0, (str.length / 2).floor] == str.reverse[0, (str.length / 2).floor]
			return true
		else
			return false
		end
	end
end
p 341.palindrome?
p 121.palindrome?
p 3456543.palindrome?
The execution result is as follows.
% ruby number_palindrome.rb
false
true
true
that's all.
:zero::one::two::three::four::five::six::seven::eight::nine::ten:
Recommended Posts