Palindrome in Scala

Solution to Project Euler problem 4

package problem4

object problem4 {
  def isPalindrome(x: String) : Boolean = {
    var i = 0
    var j = x.length() - 1
    
    while(i <= j) {
      if (x.charAt(i) != x.charAt(j))
        return false
        
        i += 1
        j -= 1
    }
    return true
  }

  def main(args: Array[String]): Unit = {
    var max = 0
    for(i <- 100 to 1000) {
      for(j <- 100 to 1000) {
        var prod = i * j
        var prodStr = prod.toString()
        if (isPalindrome(prodStr)) {
          max = Math.max(max, prod)
        }
      }
    }
    
    println(max)
  }
}

Palindrome in C++

Solution to Project Euler problem 4

#include<iostream>
using namespace std;

bool isPalindrome(char* x);

int main()
{
	char prodStr[100];
    int max = 0;
    int i, j, prod;
    for (i = 100; i < 1000; i++)
    {
        for (j = 100; j < 1000; j++)
        {
            prod = i * j;
            sprintf_s(prodStr, 100, "%i", prod);
            if(isPalindrome(prodStr) == 1)
            {
                if(prod > max)
                    max = prod;
            }
        }
    }
	cout<<max<<endl;
    return 0;
}

bool isPalindrome(char* x)
{
    int i = 0;
    int j = strlen(x) - 1;
 
    while (i <= j)
    {
        if (x[i] != x[j])
            return false;
        i++;
        j--;
    }
 
    return true;
}

Palindrome in Objective-C

Solution to Project Euler problem 4

#import <Foundation/Foundation.h>

bool isPalindrome(NSString *x)
{
    NSInteger i = 0;
    NSUInteger j = [x length] - 1;
    while (i <= j)
    {
        if ([x characterAtIndex:i] != [x characterAtIndex:j])
            return false;
        i++;
        j--;
    }
    return true;
}

int main(int argc, const char * argv[])
{
    @autoreleasepool {
        int max = 0;
        for (int i = 100; i < 1000; i++) {
            for (int j = 100; j < 1000; j++) {
                int prod = i * j;
                NSString *prodStr = [NSString stringWithFormat:@"%i", prod];
                if (isPalindrome(prodStr) && prod > max) {
                    max = prod;
                }
            }
        }
        
        NSLog(@"%i", max);
    }
    return 0;
}

Palindrome in C

Solution to Project Euler problem 4

#include<stdio.h>
#include<string.h>
#include<math.h>

int isPalindrome(char* x);

int main()
{
	char prodStr[100];
	int max = 0;
	int i, j, prod;
	for (i = 100; i < 1000; i++)
	{
		for (j = 100; j < 1000; j++)
		{
			prod = i * j;
			sprintf_s(prodStr, 100, "%i", prod);
			if(isPalindrome(prodStr) == 1)
			{
				if(prod > max)
					max = prod;
			}
		}
	}
	printf("%i\n", max);
	return 0;
}

int isPalindrome(char* x)
{
	int i = 0;
	int j = strlen(x) - 1;

	while (i <= j)
	{
		if (x[i] != x[j])
			return 0;
		i++;
		j--;
	}

	return 1;
}

Palindrome in Java

Solution to Project Euler problem 4

package projectEuler;

public class Problem4 {

	static boolean isPalindrome(String x)
    {
        int i = 0;
        int j = x.length() - 1;
        while (i <= j)
        {
            if (x.charAt(i) != x.charAt(j))
                return false;
            i++;
            j--;
        }

        return true;
    }
	
	public static void main(String[] args) {
		int max = 0;
        for (int i = 100; i < 1000; i++)
        {
            for (int j = 100; j < 1000; j++)
            {
                Integer prod = i * j;
                String prodStr = prod.toString();
                if(isPalindrome(prodStr))
                    max = Math.max(max, prod);
            }
        }
        System.out.println(max);
	}

}

Palindrome in Python

Solution to Project Euler problem 4

def isPalindrome(s):
    i = 0
    j = len(s) - 1
    
    while i < j:
        if s[i] != s[j]:
            return False
        i = i + 1
        j = j - 1 
    return True

maxVal = 0
for i in range(100, 1000):
    for j in range(100, 1000):
        prod = i * j
        prodStr = str(prod)
        if isPalindrome(prodStr) and prod > maxVal:
            maxVal = prod

print(maxVal)

Palindrome in Ruby

Solution to Project Euler problem 4

def isPalindrome(s)
  i = 0
  j = s.length - 1
  
  while i < j
    if s[i] != s[j]
      return false
    end
    i = i + 1
    j = j -1
  end
  
  return true
end

max = 0
(100..1000).each do |i|
  (100..1000).each do |j|
    prod = i * j
    prodStr = prod.to_s
    if isPalindrome(prodStr) && prod > max
      max = prod
    end
  end
end

puts(max)

Palindrome in Go

Solution to Project Euler problem 4

package main

import "fmt"

func isPalindrome(s string) bool {
	i := 0
	j := len(s) - 1
	
	for (i <= j) {
		if s[i] != s[j] {
			return false
		}
		i = i + 1
		j = j - 1
	}
	return true
}

func main() {
	var max int
	for i := 100; i < 1000; i++ {
		for j := 100; j < 1000; j++ {
			prod := i * j
			prodStr := fmt.Sprintf("%d", prod)
			if isPalindrome(prodStr) && prod > max {
				max = prod
			}
		}
	}
	println(max)
}

Palindrome in C#

Solution to Project Euler problem 4

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace problem4
{
    class Program
    {
        static bool isPalindrome(string x)
        {
            int i = 0;
            int j = x.Length - 1;
            while (i <= j)
            {
                if (x[i] != x[j])
                    return false;
                i++;
                j--;
            }

            return true;
        }

        static void Main(string[] args)
        {
            int max = 0;
            for (int i = 100; i < 1000; i++)
            {
                for (int j = 100; j < 1000; j++)
                {
                    int prod = i * j;
                    string prodStr = prod.ToString();
                    if(isPalindrome(prodStr))
                        max = Math.Max(max, prod);
                }
            }
            Console.WriteLine(max);
        }
    }
}

Palindrome in Racket

Solution to Project Euler problem 4

#lang racket
(define (isPalindrome x)
  (equal? x (reverseStr x)))
(define (ToStr x)
  (number->string x))
(define (reverseStr x)
  (list->string(reverse(string->list x))))
(define maxPal 0)
(for* ([i (in-range 100 1000)]
       [j (in-range 100 1000)])
  (let ([prod (* i j)])
    (cond [(and (isPalindrome(ToStr prod)) (> prod maxPal))
           (set! maxPal prod)])))
(printf "~a~n" maxPal)