简体   繁体   中英

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:

so for n=8 it will loop and check 8/1, 8/2 8/3 ... 8/8 the key here is the modulo (%) operator. It calculates the remainder.

Logic: We say n is divisible by m - if (n%m) is equal to zero that is remainder is zero.

You can test this code, for all n, if you take 'n' from console input: 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 ...

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

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.

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}

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 = 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 :

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

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM