[英]Write a function that prints all of the whole numbers that an integer is divisible by
What I have so far is this, but I know this is not right: 到目前为止,我的情况是这样,但是我知道这是不对的:
n=8
if n % 5 == 0:
print "divisible by 5"
elif n % 9 == 0:
print "divisible by 9"
elif n % 8 == 0:
print "divisible by 8"
n = 8
for i in range(1,n+1):
if not n%i:
print "divisible by", i
Try this: 尝试这个:
def factor_list(n):
for i in range(1,n+1):
if n%i==0:
print(i)
This method works by seeing if all the numbers up to n+1 are divisible by it: 此方法通过查看是否可以除以n + 1的所有数字来工作:
so for n=8 it will loop and check 8/1, 8/2 8/3 ... 8/8 the key here is the modulo (%) operator. 因此对于n = 8,它将循环并检查8 / 1、8 / 2 8/3 ... 8/8,这里的关键是模(%)运算符。 It calculates the remainder. 它计算余数。
Logic: We say n is divisible by m - if (n%m) is equal to zero that is remainder is zero. 逻辑:我们说n可被m整除-如果(n%m)等于零,则余数为零。
You can test this code, for all n, if you take 'n' from console input: n = int(raw_input()) 如果您从控制台输入中获取“ n”,则可以针对所有n个代码测试此代码:n = int(raw_input())
you need to go over each value from 2 up to sqrt(n) ... and see if it is divisble by that number ... 您需要遍历每个值从2到sqrt(n)...,然后看该值是否可除以该数字...
for i in range(2,int(sqrt(N))+1):
and check if something is divisible 并检查是否可分割的东西
is_divisible = number%divisor == 0
**note that all integers are divisible by 1 and them-selves **请注意,所有整数都可被1整除
More code but faster for large numbers (because you don't have to check every number, just primes) is to get all of the prime factors of n and return the product of each set in the powerset of the factors. 更多的代码,但是对于大数则更快(因为您不必检查每个数字,只需质数)就是获取n的所有质数因子,并返回因子幂集中每个集合的乘积。
import itertools as it
import functools
from operator import mul
from collections import Counter
import gmpy2 as gmpy
def prod(a):
return functools.reduce(mul, a, 1)
def get_primes(upper_limit=None):
if upper_limit:
yield from it.takewhile(lambda x: x < upper_limit, get_primes())
return
prime = 2
while True:
yield int(prime)
prime = gmpy.next_prime(prime)
def get_factors(n):
factors = Counter()
prime = get_primes()
while n != 1:
factor = next(prime)
while not n % factor:
n //= factor
factors[factor] += 1
return factors
# From here: https://docs.python.org/2/library/itertools.html#recipes
def powerset(iterable):
"powerset([1,2,3]) --> () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)"
s = list(iterable)
return it.chain.from_iterable(it.combinations(s, r) for r in range(len(s)+1))
def get_divisors(n):
factors = get_factors(n)
ret = (prod(i) for i in powerset(factors.elements()))
return set(ret)
get_divisors(81)
returns {1, 3, 9, 27, 81}
get_divisors(81)
返回{1, 3, 9, 27, 81}
get_divisors(81)
{1, 3, 9, 27, 81}
This would have looked like black magic to me when I started learning the language. 当我开始学习语言时,这对我来说就像是黑魔法。 Nonetheless; 尽管如此;
For a given n
, create the accumulator list x
对于给定的n
,创建累加器列表x
n = 162; x=[]
then loop over the integers i=2
up to i=1+n/2
and add i
to the list x
if n
is divisible by i
: 然后将整数i=2
循环到i=1+n/2
,如果n
可被i
整除,则将i
添加到列表x
:
for i in range(2,int(n/2)+1): x.extend(([],[i])[n%i==0])
or alternatively 或者
for i in range(2,int(n/2)+1): x.extend([i]) if n%i==0 else []
the result is stored in x
结果存储在x
print(x)
[2, 3, 6, 9, 18, 27, 54, 81]
All together: 全部一起:
n = 162; x=[]
for i in range(2,int(n/2)+1): x.extend(([],[i])[n%i==0])
print(x)
Explanation: PEP 308 说明: PEP 308
Edit: using list comprehension, one could even make this a one-liner: 编辑:使用列表理解,甚至可以使它成为一线式:
n=162; x=[]; x.extend([i for i in range(2,int(n/2)+1) if n%i==0]); print(x)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.