Google recruit problem

theme

Find the first prime number of 10 consecutive digits of e (base of natural logarithm) with ruby. However, e is up to 200 digits

2.7182818284590452353602874713526624977572470936999595749669676277240766303535475945713821785251664274274663919320030599218174135966290435729003342952605956307381323286279434907632338298807531952510190

Copy this into text and read it.

By the way, this is the issue here (https://qiita.com/daddygongon/items/ba94b0f2a73990fc6a07).

The code I wrote this time

For the time being, I will paste the code I wrote this time before the explanation.


def prime?(num)
  warden = 0
  i_max = Integer.sqrt(num)

  [*2..i_max].each do |i|
    warden = i
    break if num % i == 0
  end

  warden == i_max	#When I called rubocop, it became like this.It seems that you can bother to write return
end

def read_exp
  exp1 = gets.to_s.chomp
end

def google_recruit
  # e (The bottom of the natural logarithm)Read
  exp1 = read_exp

  first = 2
  last  = exp1.size - 1 - 10
  [*first..last].each do |i|
    #In the value of e,Generate consecutive 10-digit integers
    target = exp1[i..i + 10 - 1].to_i

    #Primality test
    if prime?(target)
      puts target
      break	#Break when you find the first consecutive 10-digit prime number
    end
  end
end

google_recruit if $PROGRAM_NAME == __FILE__

Commentary

The explanation is on the link I posted earlier, so I will omit it. (I wrote the code according to the explanation, so I have nothing to say)

By the way, warden == i_max Originally, that part

if warden == i_max
  return true
else
  return false
end

Was written. When I ran this on rubocop, I got warden == i_max.

Similar

Congratulations. You've made it to level 2. Go to www.Linux.org and enter Bobsyouruncle as the login and the answer to this equation as the password.

f(1)=7182818284
f(2)=8182845904
f(3)=8747135266
f(4)=7427466391
f(5)=__________

This problem is also an issue here (https://qiita.com/daddygongon/items/ba94b0f2a73990fc6a07).

Explanation of similar problems

The problem is to find a number of 10 consecutive digits of e (base of natural logarithm) with a sum of the numbers of each digit being 49. (Here (<https://q.hatena.ne) .jp/1169050304>) Seen.)

So, you can modify the code you wrote earlier. The modified code looks like this.


def sum_is49?(nums)
  sum = 0
  [*0..nums.size - 1].each do |k|
    sum += nums[k]
  end

  sum == 49
end

def read_exp
  exp1 = gets.to_s.chomp
end

######
# e (The bottom of the natural logarithm)Of 10 consecutive digits in the value of,Find the sum of the numbers in each digit is 49
#The reason why the sum wants 49,Found at the following URL
# https://q.hatena.ne.jp/1169050304
######
def google_recruit2
  # e (The bottom of the natural logarithm)Read
  exp1 = read_exp

  first = 2
  last  = exp1.size - 1 - 10
  [*first..last].each do |i|
    nums = []
    #In the value of e,Generate consecutive 10-digit integer group
    [*i..i + 10 - 1].each do |j|
      nums.push(exp1[j].to_i)
    end

    #Determine if the sum is 49
    print "#{nums.join}\n" if sum_is49?(nums)
  end
end

google_recruit2 if $PROGRAM_NAME == __FILE__

The corrected part is roughly like this.

--The number of consecutive 10 digits is made into an array with the numerical value of each digit in each element. --Instead of the function prime?, We created and used a new function sum_is49? That determines whether the sum of each element of the array is 49.

I don't write any more commentary.

By the way, nums.join returns a string that joins each element of the array nums. For details, see Here in the reference manual.

Refactoring

Refactored as pointed out by @scivola. & lt; 2020-12-29 Tue & gt; The code after refactoring looks like this.


# e (The bottom of the natural logarithm)Of 10 consecutive digits in the value of,Find the sum of the numbers in each digit is 49
def google_recruit2
  # e (The bottom of the natural logarithm)Read
  exp1 = gets.to_s.chomp

  #Generate an array of integers for each digit of e
  char_nums	= exp1.chars
  integer_nums	= char_nums.map { |n| n.to_i }

  #Generate consecutive 10-digit integer groups in the value of e.And judge whether the sum is 49,output.
  integer_nums.each_cons(10) do |nums|
    puts nums.join if nums.sum == 49
  end
end

google_recruit2 if $PROGRAM_NAME == __FILE__

Very beautiful. Good feeling.

Reference material


Recommended Posts

Google recruit problem
google recruit
google recruit
google recruit
Google Recruit
Google Recruit
Google Recruit
Google Recruit
Google recruit problem, exp and prime
EX2: Google Recruit
FizzBuzz problem
Ruby problem ⑦