Solve Google problems with Ruby

!Mac OS-11.1!ruby-2.6.6p146

Solve Google problems

problem

Find "the first prime number of 10 consecutive digits of e (base of natural logarithm)".

--Primality test

Answer

def getNums()
  line = readlines
  len = line.length
  num = ''

  (0..len-1).each do |i|
    num = num + line[i].chomp
  end

  return num
end

def prime?(num)
  (2..Math.sqrt(num).to_i).each do |i|
    if(num%i == 0)
      return false
    end
  end
  return true
end

digits = 10

if $PROGRAM_NAME == __FILE__
  num = getNums().delete('.')
  res = 0

  (0..num.length-digits).each do |i|
    res = num[i, digits].to_i
    prime?(res) ? break : next 
  end

  puts "e(The bottom of the natural logarithm)Of 10 consecutive digits in the value of,The first prime number is#{res}"
end

Commentary

--The problem was that I was inputting the number e described in the external file, so I am now using getNums () to combine the multi-line inputs. But, well, don't worry about it, you will receive standard input of numbers.

Implementation of prime?

Prime.prime? (N) can be used by require'prime', and primality test can be performed at high speed, but it is not interesting, so I will not use it this time. Implement prime? on your own.

def prime?(num)
  (2..Math.sqrt(num).to_i).each do |i|
    if(num%i == 0)
      return false
    end
  end
  return true
end

It's short! No, if you want to make it faster, you might ** play even numbers **, but for the time being, this is fine.

--Theoretically, you just see if it is divisible by a number lower than you and output true or false.

(2..Math.sqrt(num).to_i).each do |i|

(2..Math.sqrt(num).to_i).each do |i|This is the important part.
(2..num).each do |i|Theoretically, this does not end the process when there are many digits. No, it's over, but it takes too long.

Reasons why operations up to Math.sqrt (num) .to \ _i are good

Properties related to composite numbers (meaning opposite to prime numbers) If $ x $ is a composite number, we are using
with divisors less than or equal to $ \ sqrt {x} $. So if num is a composite number (= not a prime number)

(2..Math.sqrt(num).to_i).each do |i|
  if(num%i == 0)
    return false
  end
end

It gets stuck here and returns false. On the contrary, num that slips through here is not a composite number (= prime number), so it returns true.

Execution result

--Write the value of e in the text file.

2.71828182845904523536028747135266249775
7247093699959574966967627724076630353547
5945713821785251664274274663919320030599
2181741359662904357290033429526059563073
81323286279434907632338298807531952510190

I'll give this to the program.

$ ruby google_recruit.rb < in.txt

Of the 10 consecutive digits of e (base of natural logarithm), the first prime number is 7427466391.

The answer is 7427466391!

Development issues !?

It seems that this question was continued, or that the next question was prepared only for the respondents of this question. That is the next problem.

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

I see. The one who finds regularity. And I was familiar with f (1) = 7182818284.
** There was this number in e of the above problem **
f (2) = 8182845904 There is also this.

solve

Find regularity.

In what digit

Find out how many decimal places the number appears in.

def getNums()
  line = readlines
  len = line.length
  num = ''

  (0..len-1).each do |i|
    num = num + line[i].chomp
  end

  return num
end

if $PROGRAM_NAME == __FILE__
  num = getNums().delete('.')

  ["7182818284", "8182845904", "8747135266", "7427466391"].each do |i|
    pos = num.index(i)
    puts "#{i} -> #{pos} ~ #{pos+(10-1)}"
  end
end

Make it like this

$ ruby google_recruit_find.rb < google_recruit_in.txt
7182818284 -> 1 ~ 10
8182845904 -> 5 ~ 14
8747135266 -> 23 ~ 32
7427466391 -> 99 ~ 108

I don't understand at all. Lol

The guy in the TV quiz problem

** This kind of thing should be added **

def getNums()
  line = readlines
  len = line.length
  num = ''

  (0..len-1).each do |i|
    num = num + line[i].chomp
  end

  return num
end

if $PROGRAM_NAME == __FILE__
  num = getNums().delete('.')

  ["7182818284", "8182845904", "8747135266", "7427466391"].each do |n|
    sum = 0
    (0..9).each do |i|
      sum += n[i].to_i
    end
    puts "#{n} -> #{sum}"
  end
end

Make it like this

$ ruby google_recruit_sum.rb < google_recruit_in.txt
7182818284 -> 49
8182845904 -> 49
8747135266 -> 49
7427466391 -> 49

** Maybe a genius ** (Watching too much TV)
Search e for a number that makes the total of each digit (10 digits) 49, and the 5th answer is.

Answer

Write a program to look for this in Ruby and get an answer.

def getNums()
  line = readlines
  len = line.length
  num = ''

  (0..len-1).each do |i|
    num = num + line[i].chomp
  end

  return num
end

digits = 10

if $PROGRAM_NAME == __FILE__
  e = getNums().delete('.')
  num = 0
  (1..e.length-digits).each do |i|
    sum = 0
    (0..digits-1).each do |j|
      sum += e[i, digits][j].to_i
      break if sum > 49
    end
    puts "f(#{num = num.succ})=#{e[i, digits]}" if sum == 49
  end
end

Like this? ??

$ ruby google_recruit2.rb < google_recruit_in.txt
f(1)=7182818284
f(2)=8182845904
f(3)=8747135266
f(4)=7427466391
f(5)=5966290435
f(6)=2952605956

The answer is 5966290435!


Recommended Posts

Solve Google problems with Ruby
Use Ruby with Google Colab
Solve with Ruby AtCoder ABC177 D UnionFind
Solve Ruby TGIF
Procedure for operating google spreadsheet with API (ruby)
[Competition Pro] Solve the knapsack problem with Ruby
AtCoder ABC127 D hash to solve with Ruby 2.7.1
Install Ruby 3.0.0 with asdf
Getting Started with Ruby
11th, Classification with Ruby
Recent problems with rbenv
Evolve Eevee with Ruby
[At Coder] Solve the ABC183 D problem with Ruby
Solve the N + 1 problem with Ruby on Rails: acts-as-taggable-on
[At Coder] Solve the ABC182 D problem with Ruby
Ruby version switching with rbenv
I tried DI with Ruby
GraphQL Client starting with Ruby
Ruby: Send email with Starttls
Leap year judgment with Ruby
Format Ruby with VS Code
Integer check method with ruby
Ruby Learning # 8 Working With String
[Ruby] problem with if statement
Studying with CodeWar (ruby) ⑤ proc
[Ruby] REPL-driven development with pry
Getting Started with Ruby Modules
[ruby] Method call with argument
I tried to solve the problem of "multi-stage selection" with Ruby
[Tips] How to solve problems with XCode and Swift for beginners
Install Ruby on MSYS2 with pacman
Make electronic music with randomness with Ruby
Learning Ruby with AtCoder 6 [Contest 168 Therefore]
Solve M-SOLUTIONS Procon Open 2020 with java
Let's use Amazon Textract with Ruby
Programming with ruby (on the way)
Studying with CodeWar (ruby) ④ case ~ when
[Ruby] Extracting elements with slice method
Use Java 11 with Google Cloud Functions
[Google Cloud] Getting Started with Docker
[Ruby] Handle instance variables with instance methods
Problems with android studio development series
Work with Google Sheets from Java
Google Cloud Platform with Spring Boot 2.0.0
Handle DatePicker with Ruby / gtk3 + glade3
Impressions of making BlackJack-cli with Ruby
Install ruby on Ubuntu 20.04 with rbenv
Make a typing game with ruby
[Beginner] Let's solve AtCoder problem with Ruby while looking at the article!
Solve ARC104 D Multiset Mean with Scala, Java, C ++, Ruby, Perl, Elixir
I tried to solve the tribonacci sequence problem in Ruby, with recursion.