[RUBY] Google recruit problem, exp and prime

theme

{The first prime number of 10 consecutive digits of e (base of natural logarithm)}

With ruby. However, e (base of natural logarithm) is up to 200 digits

2.71828182845904523536028747135266249775
7247093699959574966967627724076630353547
5945713821785251664274274663919320030599
2181741359662904357290033429526059563073
81323286279434907632338298807531952510190

Is. Copy this into text and read it.

Commentary

Reading a text file

If you want to read the entire contents of a text file, you can use the read method. If you are worried about whether it can be read, you can display it using put.


exp=File.read("e.txt")
#->Read a text file with values ​​up to 200 digits in the base e of the natural logarithm

p exp
#->Content confirmation

How to display 10 digits at a time

Create a 10-digit integer using a while loop. To be on the safe side, convert the numbers stored in each array to integers.

#Turn the loop to cut out a 10-digit integer from the natural logarithm
i=0

while(i<200)
 j=0
 num=0
 while (j<0)
  num=10*num+exp[i+j].to_i
  j+=1
 end
i+=1
end

Primality test

We used prime, which is a library that handles prime numbers and prime factorization. Prime.prime? (Number) can determine whether the variable number is a prime number and returns true if it is a prime number, false otherwise. The function find \ _prime determines whether the variable num is a prime number, but returns num if num is a prime number and 0 if it is a composite number. Then, the returned prime numbers are put into an array that stores the ten-digit prime numbers included in the base of the natural logarithm prepared in advance. Below is the code for find \ _prime.

require 'prime' 

def find_prime(num)

 if(Prime.prime?(num)==true) then
  return num
 else
  return 0
 end
end

program

This is the source code that summarizes the above explanation.


require 'prime'

def find_prime(num)
  if(Prime.prime?(num)==true) then
    #puts 1
    return num
  else
    #puts 0
    return 0
  end
end

def count_digits(num)
  num.to_s.length
end

exp=File.read("e.txt")
p exp
#puts count_digits(exp)
#Cut out a 10-digit integer in a double loop

i=0
result=[]

while (i<200) do
  j=0
  num=0

  while (j<10) do
    num=10*num+exp[i+j].to_i
    j+=1
  end
  i+=1

  if (find_prime(num)!=0) then
    result.push(num)
  end
  #puts num
  #puts find_prime(num)
end

puts "Prime number list"
puts result
puts " "
puts "First prime number"
puts result[0]

Output result

C:\Users\nobua\M1>ruby google_recruit_problem_2.rb
"2.7182818284590452353602874713526624977572470936999595749669676277240766303535475945713821785251664274274663919320030599218174135966290435729003342952605956307381323286279434907632338298807531952510190"

Prime number list 7427466391 7413596629 6059563073 3490763233 2988075319

First prime number 7427466391

References

https://ist.ksc.kwansei.ac.jp/~nishitani/?GoogleEntExam


Recommended Posts

Google recruit problem, exp and prime
Google recruit problem
google recruit
google recruit
google recruit
google recruit
Google Recruit
Google Recruit
Google Recruit
Google Recruit
EX2: Google Recruit