I am new to programming and new to rust.
I came across leetcode.com and picked up problem #479. Largest Palindrome Product to practice.
I worked the solution in my computer and it worked just fine.
when i try to post in leetcode it throws an error.
I have my code in main function without calling any other functions.
in leetcode there is a predefined template as :
impl Solution {
pub fn largest_palindrome(n: i32) -> i32 {
}
}
how do i post my solution here in this leet code template above?
My Solution:
From the product of 2 n-digit numbers, find the largest palindrome number. where n is 1 <= n <= 8
fn main() {
let n:u32 = 2;
// match constraint for n: 1 <= n <= 8
match n {
1..=8 =>println!("'n' Witin constraint: 1 <= n <= 8"),
_ => {println!("'n' Out-Of constraint: 1 <= n <= 8\nTry Again!"); return;}
}
let smallest_n_digit_num:i64 = 10i64.pow(n-1);
let largest_n_digit_num:i64 = (10i64.pow(n))-1;
'out: for factor_one in (smallest_n_digit_num..=largest_n_digit_num).rev() {
for factor_two in (smallest_n_digit_num..=largest_n_digit_num).rev(){
// here i nested for loop from largest number gradually to smallest
// because i want to check from largest product to find largest palindrome
if factor_two > factor_one {continue;} // This removes the redudancy in the product.
let product:i64 = factor_one * factor_two; // gets product of 2 n-digit number
/*
// test println to check:
//println!(factors: {factor_one} X {factor_two} = {product} :product.");
*/
// now i will reverse the product
let mut number = product; // new variable 'number' takes the value of 'product' variable.
let mut reverse:i64 = 0; // new variable 'reverse' holds value of 0.
while number !=0 {
// 'number%10' outputs the last digit of 'number' variable
// 'reverse = (reverse * 10) + number % 10' appends last digit from 'number' variable to 'reverse' variable
reverse = (reverse * 10) + number % 10;
// after catching the last digit of 'number' variable,
// remove the last digit from 'number' variable by: '(number-(number%10)) / 10'
// then update 'number' variable, 'number = (number-(number%10)) / 10'
number = (number-(number%10)) / 10;
// now second-last digit of product variable becomes last digit of number variable
// put this in loop until 'number' variable becomes 0 and 'reverse' variable become reverse number of 'product' variable.
}
// now check for palindrome
if reverse == product {
/*
// test println to check:
//println!("palindrome is: {product}. With factors: {factor_one} & {factor_two}");
// now we catch the largest palindrome product: which is product of its largest factors.
// because our factors range from largest to smallest,
// the first palindrome found will be the largest palindrome.
*/
println!("Input: n = {n}\nOutput: {}",product % 1337);
println!("Explanation: {factor_one} x {factor_two} = {product}, {product} % 1337 = {}",product % 1337);
break 'out;
}
}
}
}
Am i missing something here? Sorry for being nuisance, if i am, it is not intended.
here is the question snip from leetcode: