/dev/Chiheb-Nexus

Solution de Projecteuler.net: Problème 5 (Go)

/*
# Project Euler: Problem 5
#
# 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?
#
##########################
# Solution: 232792560
# Best time: 223.410595ms
##########################
*/

package main
import (
    "fmt"
    "time"
)

func divided_by(number int, limit int) bool{
    for k:=11; k <= limit; k++ {
        if number % k != 0 {
            return false
        }
    }
    return true

}

func main(){
    start := time.Now()
    condition := true
    j, limit := 20, 20

    for condition {
        if divided_by(j, limit) {
            elapsed := time.Since(start)
            fmt.Printf("Solution: %v\telapsed: %v\n", j, elapsed)
            break
        }

        j+=20

    }

}

Same implementation of the same algorithm using Python:

# Project Euler: Problem 5
#
# 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?
#
###############################
# Solution: 232792560
# Best time: 7.948965787887573
###############################

from time import time

def divided_by(n, limit):
    for k in range(11, limit+1):
        if n % k != 0:
            return False
    return True

def get_number(start, divide):
    condition = True
    while condition:
        if divided_by(start, divide):
            return start
        start += 20 

start, divide = 20, 20
s = time()
number = get_number(start, divide)
elapsed = time() - s
print("Solution: {0} \tTime elapsed: {1}".format(number, elapsed))