Project Euler – Problem #’s 1, 2, 3, 4, 5, 6, 7 – Solved with Javascript

js

Project Euler – Problem # 1 – Solved with Javascript:

Project Euler – Problem # 2 – Solved with Javascript:

Project Euler – Problem # 3 – Solved with Javascript:

Project Euler – Problem # 4 – Solved with Javascript:

Project Euler – Problem # 5 – Solved with Javascript:

Project Euler – Problem # 6 – Solved with Javascript:

Project Euler – Problem # 7 – Solved with Javascript:

Advertisements

Project Euler – Problem # 4 – Solved with the Go programming language

Find the largest palindrome made from the product of two 3-digit numbers.

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.

One Possible Solution: Go

package main

import "fmt"
import "strconv"
import "strings"

func isPalindrome(product int) (isP bool) {
	s := strconv.Itoa(product)
	a := strings.Split(s, "")
	L := len(a)
	for i := 0; i < L/2; i++ {
		a[i], a[L-i-1] = a[L-i-1], a[i]
	}
	x := strings.Join(a, "")
	y, _ := strconv.Atoi(x)
	if product == y {
		return true
	}
	return false

}

func main() {
	largest := 0
	for i := 1; i < 1000; i++ {
		for j := 1; j < 1000; j++ {
			product := i * j
			if isPalindrome(product) {
				if product > largest {
					largest = product
				}

			}
		}

	}
	fmt.Println(largest)
}

Project Euler – Problem # 4 – Solved with Java & Python

Find the largest palindrome made from the product of two 3-digit numbers.

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.

**********

The way to reverse a sting in Python is a lot shorter than the way to do it in Java. y = x[::-1] as opposed to the reverse method in the Java program.

One Possible Solution: Java


public class Problem_4 {
	public static void main(String args[]){
		Problem_4 prob_4Obj = new Problem_4();
		int largest = 0;
		for (int i = 100; i < 1000; i++){
			for (int r = 100; r < 1000; r++){
				String x = Integer.toString(i * r);
				String y = prob_4Obj.reverse(x);
				int xx = Integer.parseInt((x));
				if (x.equals(y)) {
					if (xx > largest){
						largest = (xx);
					}
				}
					
				}
			}
	System.out.println(largest);	
	}
		
// http://www.vbforums.com/showthread.php?t=320907
	 public String reverse(String arg)
	 {
		String tmp = null;
		if (arg.length() == 1)
		{
			return arg;
		}
		
		else
		{
			String lastChar = arg.substring(arg.length() -1, arg.length());
			
			String remainingString = arg.substring(0, arg.length() -1);
	
			tmp = lastChar + reverse(remainingString);
			return tmp;
			
			
		}
	  }
// http://www.vbforums.com/showthread.php?t=320907
}

One Possible Solution: Python

# Python version = 2.7.2
# Platform = win32

def findLargest():
    largest = 0
    for i in range (100, 999):
        for r in range(100, 999):
            x = str(i * r)
            y = x[::-1]
            if x == y:
                x = int(x)
                if x > largest:
                    largest = (x)
    return largest

def main():
    print findLargest()
        
if __name__ == '__main__':
    main()