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 # 5 – Solved with the Go programming language

What is the smallest number divisible by each of the numbers 1 to 20?

2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.

What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?

One Possible Solution: Go

package main

import (
	"fmt"
)

func evenlyDivisible(n int) (eD int) {
	/*Checks if number (n) is evenly divisible by the numbers 1 -20.*/

	counter := 0
	for i := 1; i < 21; i++ {
		if n%i == 0 {
			counter += 1
			if counter == 20 {
				return n
			}

		} else {
			return 1
		}
	}
	return 1
}
func main() {
	/*Main program - since we know that 2520 is evenly divisible by
	the first 10 integers, we know that the number in question is a
	multiple of 2520. So we start with 2520 and increment by that much
	until we find the smallest number that is evenly divisible by the
	first 20 integers (1-20)*/

	n := 2520
	Switch := 1
	for Switch >= 1 {
		smallest := evenlyDivisible(n)
		if smallest != 1 {
			fmt.Println(smallest)
			Switch = 0

		} else {
			n += 2520
		}
	}
}

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

What is the smallest number divisible by each of the numbers 1 to 20?

2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.

What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?

**********

“””Main program – since we know that 2520 is evenly divisible by
the first 10 integers, we know that the number in question is a
multiple of 2520. So we start with 2520 and increment by that much
until we find the smallest number that is evenly divisible by the
first 20 integers (1-20)”””

One Possible Solution: Java


public class Problem_5 {
	public static void main(String args[]){
		Problem_5 prob_5Obj = new Problem_5();
		int n = 2520;
		int Switch = 1;
		while (Switch == 1){
			Integer smallest = prob_5Obj.evenlyDivisible(n);
			if(smallest != 1){
				System.out.println(smallest);
				Switch = 0;
			}else{
				n += 2520;
			}
		}
	}
	
	public Integer evenlyDivisible(int n)
	{
		int counter = 0;
		for (int i = 1; i < 21; i++)
		{
			if(n % i == 0)
			{
				counter = counter + 1;
				if (counter == 20){
					return n;
				}
				else{
					continue;
				}
			}
			else{
				return 1;
			}
		}
		return 1;
	}
}

One Possible Solution: Python

# Python Version = 2.7.2
# Platform = win32
# Run time =  0.442848859867

import time

def evenlyDivisible(n):
    """Checks if number (n) is evenly divisible by the numbers 1 -20."""
    counter = 0
    for i in range(1, 21):
        if n % i == 0:
            counter = counter + 1
            if counter == 20:
                return n
        else:
            return 1

def main():
    """Main program - since we know that 2520 is evenly divisible by
    the first 10 integers, we know that the number in question is a
    multiple of 2520. So we start with 2520 and increment by that much
    until we find the smallest number that is evenly divisible by the
    first 20 integers (1-20)"""
    start_time = time.clock()
    n = 2520
    switch = 1
    while switch == 1:
        smallest = evenlyDivisible(n)
        if smallest != 1:
            print smallest
            switch = 0
        else:
            n += 2520
    run_time = time.clock() - start_time
    print "Run time = ", run_time
    
if __name__ == '__main__':
    main()