I was trying to solve project Euler problem 4 which is:
A palindromic number reads the same both ways. The largest palindrome made from the product of tw
You are also assuming that the first palindrome you'll find will be the largest. The first palindrome you'll find is 580085 which isn't the right answer.
You are also assuming that the first palindrome you'll find is the product of two 3 digit numbers. You should also use two different numbers instead of multiplying 999 with 999 and iterating down to 100 * 100.
Find the largest palindrome made from the product of two 3-digit numbers. R doesn't have a built-in function to check palindromes so I created one although 'rev' can be used :). Also, the code is not optimized for speed on purpose primarily to increase readability.
reverse <- function(x){
Args:
x : object whose elements are to be reversed, can be of type
'character' or 'vector' of length = 1
Returns:
x : The object's elements are reversed e.g boy becomes yob and 23
becomes 32
Error Handling:
if (is.vector(x) == TRUE & length(x) > 1){
stop("Object whose length > 1 cannot be used with reverse(x)
consider vector.reverse(x)")
}
Function Execution
if (is.character(x) == TRUE){
v <- unlist(strsplit(x, ''))
N <- length(v)
rev.v <- v
for (i in 0:(N - 1)){
rev.v[i + 1] <- v[N - i]
}
rev.v <- paste0(rev.v, collapse = '')
return(rev.v)
} else {
x <- as.character(x)
v <- unlist(strsplit(x, ''))
rev.v <- v
N <- length(v)
for (i in 0:(N - 1)){
rev.v[i + 1] <- v[N - i]
}
rev.v <- paste0(rev.v, collapse = '')
rev.v <- as.numeric(rev.v)
return(rev.v)
}
}
the function vector.reverse() has been deleted to reduce the length of
this code
is.palindrome <- function(x){
Args:
x : vector whose elements will be tested for palindromicity, can be of
length >= 1
Returns:
TRUE : if an element in x or x is palindromic
FALSE: if an element in x or x is not palindromic
Function Execution:
if (is.vector(x) == TRUE & length(x) > 1){
x.prime <- vector(length = length(x))
for (i in 1:length(x)){
x.prime [i] <- reverse(x [i])
}
return(x.prime == x)
} else {
ifelse(reverse(x) == x, return(TRUE), return(FALSE))
}
}
palindromes between 10000 and 999*999
Palin <- (100*100):(999*999)
Palin <- Palin [is.palindrome(Palin) == 1]
i.s <- vector('numeric', length = length(Palin))
j.s <- vector('numeric', length = length(Palin))
Factoring each of the palindromes
for (i in 100:999){
for (j in 100:999){
if (sum(i * j == Palin) == 1){
i.s[i-99] <- i
j.s[i-99] <- j
}
}
}
product <- i.s * j.s
which(i.s * j.s == max(product)) -> Ans
paste(i.s[Ans[1]], "and", j.s[Ans[1]], "give the largest two 3-digit
palindrome")
ANSWER
993 * 913 = 906609
ENJOY!
Another simple solution written in C#
private static void Main(string[] args)
{
var maxi = 0;
var maxj = 0;
var maxProd = 0;
for (var i = 999; i > 100; i--)
for (var j = 999; j > 100; j--)
{
var product = i * j;
if (IsPalindrome(product))
if (product > maxProd)
{
maxi = i;
maxj = j;
maxProd = product;
}
}
Console.WriteLine(
"The highest Palindrome number made from the product of two 3-digit numbers is {0}*{1}={2}", maxi, maxj,
maxProd);
Console.ReadKey();
}
public static bool IsPalindrome(int number)
{
var numberString = number.ToString();
var reverseString = string.Empty;
for (var i = numberString.Length - 1; i >= 0; --i)
reverseString += numberString[i];
return numberString == reverseString;
}