Solved ProjectEuler/004
This commit is contained in:
17
ProjectEuler/004/desc.yml
Normal file
17
ProjectEuler/004/desc.yml
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
title: Find the largest palindrome made from the product of two 3-digit numbers.
|
||||||
|
url: http://projecteuler.net/problem=4
|
||||||
|
|
||||||
|
desc: |
|
||||||
|
A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 99.
|
||||||
|
Find the largest palindrome made from the product of two 3-digit numbers.
|
||||||
|
|
||||||
|
solution: |
|
||||||
|
See code
|
||||||
|
|
||||||
|
solutions:
|
||||||
|
solve.php:
|
||||||
|
desc: Basic solution
|
||||||
|
language: php
|
||||||
|
solve.rb:
|
||||||
|
desc: Basic solution in Ruby
|
||||||
|
language: ruby
|
13
ProjectEuler/004/solve.php
Normal file
13
ProjectEuler/004/solve.php
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
<?php
|
||||||
|
$max = 0;
|
||||||
|
for($num1 = 1000; $num1>100; $num1--) {
|
||||||
|
for($num2 = 1000; $num2>100; $num2--) {
|
||||||
|
$sum = $num1 * $num2;
|
||||||
|
|
||||||
|
//Check if palindrome
|
||||||
|
if($sum > $max AND strrev($sum) == $sum)
|
||||||
|
$max = $sum;
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
echo $max;
|
10
ProjectEuler/004/solve.rb
Normal file
10
ProjectEuler/004/solve.rb
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
max = 0;
|
||||||
|
(100..1000).each do |num1|
|
||||||
|
(100..1000).each do |num2|
|
||||||
|
sum = num1 * num2
|
||||||
|
if( sum > max and sum.to_s.reverse == sum.to_s)
|
||||||
|
max = sum
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
print max
|
Reference in New Issue
Block a user