HACKER RANK Project Euler 56 - Powerful Digit Sum - Solution.'Rb'
Project Euler Challenges-56 - Powerful Digit Sum Solution
Problem Statement
This problem is a programming version of Problem 56 from projecteuler.net
A googol (10100) is a massive number: one followed by one-hundred zeros. 100100 is almost unimaginably large: one followed by two-hundred zeros. Despite their size, the sum of the digits in each number is only 1.
Considering natural numbers of the form, ab , where a,b<N , what is the maximum digital sum?
Input Format
Input contains an integer N
Output Format
Print the answer corresponding to the test case.
Constraints
5≤N≤200
Sample Input
5
Sample Output
13
Explanation
44=256 and 2+5+6=13 , which is the maximum digital sum for this range.
Problem Statement
This problem is a programming version of Problem 56 from projecteuler.net
A googol (10100) is a massive number: one followed by one-hundred zeros. 100100 is almost unimaginably large: one followed by two-hundred zeros. Despite their size, the sum of the digits in each number is only 1.
Considering natural numbers of the form, ab , where a,b<N , what is the maximum digital sum?
Input Format
Input contains an integerN
Input contains an integer
Output Format
Print the answer corresponding to the test case.
Print the answer corresponding to the test case.
Constraints
5≤N≤200
Sample Input
5
Sample Output
13
Explanation
44=256 and 2+5+6=13 , which is the maximum digital sum for this range.
Solution:
# Enter your code here. Read input from STDIN. Print output to STDOUT
num=gets
max=0
for i in 1...num.to_i
for j in 1...num.to_i
sum=i**j
#print sum
#print "\t"
k=sum
while k.to_i>0 do
c=k.to_i%10
d= d.to_i + c.to_i
k=k.to_i/10
end
#print d.to_i
#print "\n"
if d.to_i>max then
max=d.to_i
d=0
end
d=0
end
end
print max
# Enter your code here. Read input from STDIN. Print output to STDOUT
num=gets
max=0
for i in 1...num.to_i
for j in 1...num.to_i
sum=i**j
#print sum
#print "\t"
k=sum
while k.to_i>0 do
c=k.to_i%10
d= d.to_i + c.to_i
k=k.to_i/10
end
#print d.to_i
#print "\n"
if d.to_i>max then
max=d.to_i
d=0
end
d=0
end
end
print max
Comments
Post a Comment