Project Euler #4: Largest palindrome product

Sort by

recency

|

304 Discussions

|

  • + 0 comments
    bool isPalindrome(int n) {
        string fwd(to_string(n));
        string rev(to_string(n));
        std::reverse(rev.begin(), rev.end());
        return (fwd == rev);
    }
    
    void findPalindromeFactors(int n) {
        int max = 0;
        for (int i = 100; i < 1000; i++) {
            for (int j = i; j < 1000; j++) {
                int prod = i * j;
                if (prod < n) {
                    if (prod > max && isPalindrome(prod)) {
                        max = prod;
                    }
                }
            }
        }
        
        cout << max << endl;
    }
    
  • + 0 comments

    Haskell

    import Control.Applicative
    import Control.Monad
    import System.IO
    import Data.Set (Set)
    import qualified Data.Set as Set
    
    palindrome :: [Int]
    palindrome = [x*y |
        y <- [100 .. 999], 
        x <- [y .. 999],  
        (x*y) <= 1000000, 
        (x*y) == read (reverse (show (x*y)))]
        
    cap :: [Int] -> Int -> [Int]    
    cap xs n = filter (<n) xs
        
    findMax :: [Int] -> Int
    findMax [x] = x
    findMax (x:y:xs)
        | x > y = findMax (x:xs)
        | otherwise = findMax (y:xs)
    
    main :: IO ()
    main = do
        t_temp <- getLine
        let t = read t_temp :: Int
        forM_ [1..t] $ \a0  -> do
            n_temp <- getLine
            let n = read n_temp :: Int
            let palList = cap palindrome n
            putStrLn(show (findMax palList))
    
  • + 0 comments

    ****def l_pal(n): s=[] for i in range(101,1000): for j in range(121,1000,(1 if i%11==0 else 11)): product = i*j if int(str(product)[::-1]) == product: if product

    if name == 'main': t = int(input().strip())

    for t_itr in range(t):
        n = int(input().strip())
        print(l_pal(n))
    
  • + 0 comments

    C++ code

    #include <cmath>
    #include <cstdio>
    #include <iostream>
    using namespace std;
    
    bool palindrome(int n){
        string s(to_string(n));
        int l= s.length();
        for(int i=0;i<l/2;i++){
            if(s[i] != s[l-i-1]) return false;
        }
        return true;
    }
    
    int find_max(int n){
        int max= 0;
        int produit;
        for(int n1=100;n1<1000;n1++){
            for(int n2=n1;n2<1000;n2++){
                produit=n1*n2;
                if(produit > max && produit < n && palindrome(produit))
                   max=produit;
           }
        }
        return max;
    }
    
    
    int main() {
        int T, N;
        cin >> T;
        for(int i=0; i< T; i++){
            cin >> N;
            cout << find_max(N) << endl;
        }   
        return 0;
    }
    
  • + 0 comments

    def is_palindrome(n): return str(n) == str(n)[::-1] def checker(k): for i in range(999, 99, -1):
    if k % i == 0 and 100 <= k // i <= 999: return True return False

    def main(): t = int(input()) for _ in range(t): n = int(input()) for i in range(n - 1, 101101 - 1, -1): if is_palindrome(i) and checker(i): print(i) break

    if name == "main": main()