PE1-3

Problem 1

If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.

Find the sum of all the multiples of 3 or 5 below 1000.

package main

import "fmt"

func main() {
    result := 0
    fmt.Println("1000以内3和5的倍数之和")
    for i := 0; i < =1000; i++ {
        if i%3 == 0 && i%5 == 0 {
            result += i
        }
    }
    fmt.Println(result)
}

结果:233168

Problem 2

Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:

1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...

By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.

package main

import "fmt"

func main() {
i := 1
j := 2
sum := 0
for {
temp := i + j
i = j
j = temp
if j%2 == 0 {
sum += j
}
if j > 4000000 {
break
}
}
fmt.Println(sum + 2)
}

结果:4613732

Problem 3

The prime factors of 13195 are 5, 7, 13 and 29.

What is the largest prime factor of the number 600851475143 ?


package main

import (
"fmt"
"os"
)

func main() {

a1 := 600851475143
a2 := 10
saun(a2, a1)
fmt.Println(a1)
}

func saun(a2, a1 int) {
temp := 0
m := 1
for i := 2; i < a2; i++ {
for j := 2; j < i; j++ {
temp = j
if i%j == 0 {
break
}
}
if temp == i-1 {
if a1%i == 0 {
if a1/i == 1 {
fmt.Println(a1)
os.Exit(0)
}
m = a1 / i
a1 = m
m = 1
}
}
}
saun(a2*10, a1)
}
 

答案:6857

https://necydcy.me/
原文地址:https://www.cnblogs.com/miria-486/p/10085225.html