【每日一题20220624】3 或 5 的倍数

:mage:‍如果我们列出所有小于 10 且是 3 或 5 的倍数的自然数,我们会得到 3、5、6 和 9。这些倍数之和是 23。

设计一个方法,使其返回传入数字以下的所有 3 或 5 的倍数之和。此外,如果数字为负数,则返回 0。

注意:如果数字是 3 和 5 的倍数, 则只计算一次。

【示例】
输入:10
输出:23
解释:列出所有小于 10 且是 3 或 5 的倍数的自然数,我们会得到 3、5、6 和 9。这些倍数之和是 23。

题目难度:简单
题目来源:Multiples of 3 or 5 | Codewars


def solution(number):
    pass

assert solution(10) == 23
assert solution(-1) == 0
assert solution(15) == 45
assert solution(200) == 9168
def solution(number: int) -> int:
    result = 0
    i = 1
    if number < 0:
        return 0
    else:
        while i < number:
            if i % 3 == 0 or i % 5 == 0:
                result += i
            i += 1
        return result


assert solution(10) == 23
assert solution(-1) == 0
assert solution(15) == 45
assert solution(200) == 9168
def solution(number: int) -> int:
    return sum(i for i in range(number) if i % 3 == 0 or i % 5 == 0)
def solution(number):
    return sum([i for i in range(number) if i % 3==0 or i % 5 == 0])

assert solution(10) == 23
assert solution(-1) == 0
assert solution(15) == 45
assert solution(200) == 9168
def solution(number):
    return sum([i for i in range(1, number) if i % 3 == 0 or i % 5 == 0]) if number>0 else 0
def solution(number):
    return sum([i for i in range(1,number) if i%3==0 or i%5==0]) if number>0 else 0

assert solution(10) == 23
assert solution(-1) == 0
assert solution(15) == 45
assert solution(200) == 9168
def solution(number: int)-> int:
    result = [i for i in range(1, number) if i % 3 == 0 or i % 5 == 0]
    result = sum(result)
    return result


if __name__ == '__main__':
    assert solution(10) == 23
    assert solution(-1) == 0
    assert solution(15) == 45
    assert solution(200) == 9168
def solution(number):
    if number < 0:
        return 0
    s = []
    for i in range(1, number):
        if i % 3 == 0 or i % 5 == 0:
            s.append(i)
    return sum(s)
    # return sum([i for i in range(1, number) if (i % 3 == 0 or i % 5 == 0)]) if number > 0 else 0


assert solution(10) == 23
assert solution(-1) == 0
assert solution(15) == 45
assert solution(200) == 9168
def solution(number):
    if number<0:
        return 0
    s=set(i for i in range(3, number,3))
    s.update(set(i for i in range(5, number,5)))
    return sum(s)

def solution(number:int):
result = 0
if number <= 0:
return 0
else:
for i in range(number):
if i % 3 ==0 or i % 5 ==0:
result += i
return result

solution(10)

def solutions(number:int):
return sum([i for i in range(number) if i % 3 ==0 or i % 5 ==0])

print(solutions(10))

assert solution(10) == 23
assert solution(-1) == 0
assert solution(15) == 45
assert solution(200) == 9168

def solution(number):
    if number <= 0:
        return 0
    else:
        result = 0
        for i in range(number):
            if i % 3 == 0 or i % 5 == 0:
                result += i
        return result
def solution(number):
    res_list = []
    for i in range(0, number):
        if (i % 3 == 0) or (i % 5 == 0):
            res_list.append(i)
    return sum(res_list)


assert solution(10) == 23
assert solution(-1) == 0
assert solution(15) == 45
assert solution(200) == 9168

public class Multiple {
public int test(int n) {
int sum = 0;
if (n < 0) {
return 0;
}
for (int i = 1; i <= n; i++) {
if ((i % 3 == 0)||(i % 5 == 0)) {
sum = sum + i;
System.out.println(sum);
}
}
return sum;
}
}

# O(1)
def solution(number: int) -> int:
    three_b = (number - 1) // 3
    five_b = (number - 1) // 5
    common_b = (number - 1) // 15

    a = (3 + three_b * 3) / 2 * three_b
    b = (5 + five_b * 5) / 2 * five_b
    c = (15 + common_b * 15) / 2 * common_b
    return int(a + b - c)
@Test
    @DisplayName("3或5的倍数")
    public void Test(){
        assert erLiuTest(10)==23;
        System.out.println(erLiuTest(10));
        assert erLiuTest(-1)==0;
        System.out.println(erLiuTest(-1));
        assert erLiuTest(15)==45;
        System.out.println(erLiuTest(15));
        assert erLiuTest(200)==9168;
        System.out.println(erLiuTest(200));
    }
    public int erLiuTest(int input){
        int sum=0;
        if(input>=0){
            for(int i=1;i<input;i++){
                if(i%3==0||i%5==0){
                    sum=sum+i;
                }
            }
        }else {
            return 0;
        }
        return sum;
    }
关闭