I need to test whether each number from 1 to 1000 is a multiple of 3 or a multiple of 5. The way I thought I'd do this would be to divide the number by 3, and if the result is an integer then it would be a multiple of 3. Same with 5.
我需要测试从1到1000的每个数字是3的倍数还是5的倍数。我认为我这样做的方法是将数字除以3,如果结果是整数那么它会是3的倍数。与5相同。
How do I test whether the number is an integer?
如何测试数字是否为整数?
here is my current code:
这是我目前的代码:
n = 0
s = 0
while (n < 1001):
x = n/3
if isinstance(x, (int, long)):
print 'Multiple of 3!'
s = s + n
if False:
y = n/5
if isinstance(y, (int, long)):
s = s + n
print 'Number: '
print n
print 'Sum:'
print s
n = n + 1
9 个解决方案
#1
156
You do this using the modulus operator, %
您可以使用模数运算符执行此操作,%
n % k == 0
evaluates true if and only if n
is an exact multiple of k
. In elementary maths this is known as the remainder from a division.
当且仅当n是k的精确倍数时才计算true。在小学数学中,这被称为分裂的余数。
In your current approach you perform a division and the result will be either
在您当前的方法中,您执行除法,结果将是
- always an integer if you use integer division, or
- 如果使用整数除法,则始终为整数,或
- always a float if you use floating point division.
- 如果使用浮点除法,它总是一个浮点数。
It's just the wrong way to go about testing divisibility.
这是测试可分性的错误方法。
#2
2
You can simply use %
Modulus operator to check divisibility.
For example: n % 2 == 0
means n is exactly divisible by 2 and n % 2 != 0
means n is not exactly divisible by 2.
您只需使用%Modulus运算符来检查可分性。例如:n%2 == 0表示n可以被2整除,n%2!= 0表示n不能完全被2整除。
#3
0
You can use % operator to check divisiblity of a given number
您可以使用%运算符来检查给定数字的可分数
The code to check whether given no. is divisible by 3 or 5 when no. less than 1000 is given below:
检查是否给出的代码。当没有时,可以被3或5整除。下面给出的不到1000:
n=0
while n<1000:
if n%3==0 or n%5==0:
print n,'is multiple of 3 or 5'
n=n+1
#4
0
This code appears to do what you are asking for.
此代码似乎可以满足您的要求。
for value in range(1,1000):
if value % 3 == 0 or value % 5 == 0:
print(value)
Or something like
或类似的东西
for value in range(1,1000):
if value % 3 == 0 or value % 5 == 0:
some_list.append(value)
Or any number of things.
或任何数量的东西。
#5
0
I had the same approach. Because I didn't understand how to use the module(%) operator.
我有同样的方法。因为我不明白如何使用模块(%)运算符。
6 % 3 = 0 *This means if you divide 6 by 3 you will not have a remainder, 3 is a factor of 6.
6%3 = 0 *这意味着如果你将6除以3则不会有余数,3则是6。
Now you have to relate it to your given problem.
现在你必须将它与你给定的问题联系起来。
if n % 3 == 0 *This is saying, if my number(n) is divisible by 3 leaving a 0 remainder.
如果n%3 == 0 *这就是说,如果我的数字(n)可被3整除,则留下0余数。
Add your then(print, return) statement and continue your
添加你的then(打印,返回)语句并继续你的
#6
-2
For small numbers n%3 == 0
will be fine. For very large numbers I propose to calculate the cross sum first and then check if the cross sum is a multiple of 3:
对于小数字,n%3 == 0将没问题。对于非常大的数字,我建议先计算交叉和,然后检查交叉和是否是3的倍数:
def is_divisible_by_3(number):
if sum(map(int, str(number))) % 3 != 0:
my_bool = False
return my_bool
#7
-3
Try this ...
尝试这个 ...
public class Solution {
public static void main(String[] args) {
long t = 1000;
long sum = 0;
for(int i = 1; i<t; i++){
if(i%3 == 0 || i%5 == 0){
sum = sum + i;
}
}
System.out.println(sum);
}
}
#8
-4
jinja2 template fizzbuz:
jinja2模板fizzbuz:
<form>
<ol>
{% for x in range(1,n+1) %}
{% set fizzbuzz_rpm = x %}
{% if x % 3 == 0 and x % 5 == 0 %}
{% set fizzbuzz_rpm="FizzBuzz" %}
{% elif x % 3 == 0 %}
{% set fizzbuzz_rpm="Fizz" %}
{% elif x %5 == 0 %}
{% set fizzbuzz_rpm="Buzz" %}
{% endif %}
<li>{{fizzbuzz_rpm}}</li>
{% endfor %}
</ol>
</form>
#9
-5
The simplest way is to test whether a number is an integer is int(x) == x
. Otherwise, what David Heffernan said.
最简单的方法是测试数字是否为整数是int(x)== x。否则,大卫赫弗南说。
#1
156
You do this using the modulus operator, %
您可以使用模数运算符执行此操作,%
n % k == 0
evaluates true if and only if n
is an exact multiple of k
. In elementary maths this is known as the remainder from a division.
当且仅当n是k的精确倍数时才计算true。在小学数学中,这被称为分裂的余数。
In your current approach you perform a division and the result will be either
在您当前的方法中,您执行除法,结果将是
- always an integer if you use integer division, or
- 如果使用整数除法,则始终为整数,或
- always a float if you use floating point division.
- 如果使用浮点除法,它总是一个浮点数。
It's just the wrong way to go about testing divisibility.
这是测试可分性的错误方法。
#2
2
You can simply use %
Modulus operator to check divisibility.
For example: n % 2 == 0
means n is exactly divisible by 2 and n % 2 != 0
means n is not exactly divisible by 2.
您只需使用%Modulus运算符来检查可分性。例如:n%2 == 0表示n可以被2整除,n%2!= 0表示n不能完全被2整除。
#3
0
You can use % operator to check divisiblity of a given number
您可以使用%运算符来检查给定数字的可分数
The code to check whether given no. is divisible by 3 or 5 when no. less than 1000 is given below:
检查是否给出的代码。当没有时,可以被3或5整除。下面给出的不到1000:
n=0
while n<1000:
if n%3==0 or n%5==0:
print n,'is multiple of 3 or 5'
n=n+1
#4
0
This code appears to do what you are asking for.
此代码似乎可以满足您的要求。
for value in range(1,1000):
if value % 3 == 0 or value % 5 == 0:
print(value)
Or something like
或类似的东西
for value in range(1,1000):
if value % 3 == 0 or value % 5 == 0:
some_list.append(value)
Or any number of things.
或任何数量的东西。
#5
0
I had the same approach. Because I didn't understand how to use the module(%) operator.
我有同样的方法。因为我不明白如何使用模块(%)运算符。
6 % 3 = 0 *This means if you divide 6 by 3 you will not have a remainder, 3 is a factor of 6.
6%3 = 0 *这意味着如果你将6除以3则不会有余数,3则是6。
Now you have to relate it to your given problem.
现在你必须将它与你给定的问题联系起来。
if n % 3 == 0 *This is saying, if my number(n) is divisible by 3 leaving a 0 remainder.
如果n%3 == 0 *这就是说,如果我的数字(n)可被3整除,则留下0余数。
Add your then(print, return) statement and continue your
添加你的then(打印,返回)语句并继续你的
#6
-2
For small numbers n%3 == 0
will be fine. For very large numbers I propose to calculate the cross sum first and then check if the cross sum is a multiple of 3:
对于小数字,n%3 == 0将没问题。对于非常大的数字,我建议先计算交叉和,然后检查交叉和是否是3的倍数:
def is_divisible_by_3(number):
if sum(map(int, str(number))) % 3 != 0:
my_bool = False
return my_bool
#7
-3
Try this ...
尝试这个 ...
public class Solution {
public static void main(String[] args) {
long t = 1000;
long sum = 0;
for(int i = 1; i<t; i++){
if(i%3 == 0 || i%5 == 0){
sum = sum + i;
}
}
System.out.println(sum);
}
}
#8
-4
jinja2 template fizzbuz:
jinja2模板fizzbuz:
<form>
<ol>
{% for x in range(1,n+1) %}
{% set fizzbuzz_rpm = x %}
{% if x % 3 == 0 and x % 5 == 0 %}
{% set fizzbuzz_rpm="FizzBuzz" %}
{% elif x % 3 == 0 %}
{% set fizzbuzz_rpm="Fizz" %}
{% elif x %5 == 0 %}
{% set fizzbuzz_rpm="Buzz" %}
{% endif %}
<li>{{fizzbuzz_rpm}}</li>
{% endfor %}
</ol>
</form>
#9
-5
The simplest way is to test whether a number is an integer is int(x) == x
. Otherwise, what David Heffernan said.
最简单的方法是测试数字是否为整数是int(x)== x。否则,大卫赫弗南说。