0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

[Python][Julia]Project euler1 (プロジェクトオイラー1)

Last updated at Posted at 2023-12-23

Project euler Ploblem 1 (プロジェクトオイラー1)

Multiples of 3 or 5

備忘のために残しておく。

問題文

10未満の自然数のうち、3または5の倍数をリストアップすると、3,5,6,9となる。
これら倍数の和は23である。
1000未満の3または5の倍数の和を求めよ。

(原文)
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.

答え

233168

pythonのコード

Ploblem1(Python)
def sum_multiples(limit):
    ans = 0
    for i in range(1,limit):
        if i % 3 == 0:
            ans += i
        elif i % 5 == 0:
            ans += i
    return ans

print(sum_multiples(1000))

Juliaのコード

Ploblem1(Julia)
function sum_multiples(limit)
    ans = 0
    for i in 1:limit - 1
        if i % 3 == 0
            ans += i
        elseif i % 5 == 0
            ans += i
        end
    end
    return ans
end

println(sum_multiples(1000))
0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?