Amicable numbers, perfect numbers, excess numbers, deficient numbers, palindromic numbers I tried to make various numbers of programs

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:

Generate divisors

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]

Generate amicable numbers

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

Determine if it is a perfect number, an excess number, or a shortage number

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"

Determine if it is the number of palindromes

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

Amicable numbers, perfect numbers, excess numbers, deficient numbers, palindromic numbers I tried to make various numbers of programs
Collatz number, Fibonacci number, triangular number I tried to make various sequence programs
I tried to make a client of RESAS-API in Java
I tried to make a parent class of a value object in Ruby
I tried to make full use of the CPU core in Ruby
I tried to make Basic authentication with Java
I tried to chew C # (basic of encapsulation)
I tried to make a message function of Rails Tutorial extension (Part 1): Create a model
I tried to summarize the state transition of docker
05. I tried to stub the source of Spring Boot
I tried to reduce the capacity of Spring Boot
I tried to make a login function in Java
I tried to make FizzBuzz that is uselessly flexible
I tried to summarize various link_to used this time
I tried to make a reply function of Rails Tutorial extension (Part 3): Corrected a misunderstanding of specifications
I tried to make the sample application into a microservice according to the idea of the book "Microservice Architecture".
I tried to make a message function of Rails Tutorial extension (Part 2): Create a screen to display
I tried node-jt400 (Programs)
I tried to make an introduction to PHP + MySQL with Docker
I tried to summarize the basics of kotlin and java
I want to make a specific model of ActiveRecord ReadOnly
I tried to make Venn diagram an easy-to-understand GIF animation
I tried to verify this and that of Spring @ Transactional
[Swift] I tried to implement the function of the vending machine
I tried using Hotwire to make Rails 6.1 scaffold a SPA
I tried to make Java Optional and guard clause coexist
I tried to summarize the basic grammar of Ruby briefly
I tried to build the environment of WSL2 + Docker + VSCode
I tried to make it possible to set the delay for the UDP client of Android by myself
[Rails] Implementation of multi-layer category function using ancestry "I tried to make a window with Bootstrap 3"
I tried to make the "Select File" button of the sample application created in the Rails tutorial cool
I tried to make a machine learning application with Dash (+ Docker) part2 ~ Basic way of writing Dash ~