Find "the first prime number of 10 consecutive digits of e (base of natural logarithm)".
--Primality test
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
--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.
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|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.
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.
--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!
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.
Find regularity.
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
** 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.
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