简体   繁体   English

编写一个函数,打印一个整数可除的所有整数

[英]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.

相关问题 程序将所有可被99整除的数字写入文本文件不起作用? - Program to write all the numbers divisible by 99 to a text file not working? 如何编写打印出从 1 到输入数字的所有正 integer 值并且每对数字翻转的程序? - How to write the program that prints out all the positive integer values from 1 up to the input number and each pair of numbers is flipped? Numpy 计算整除数和余数 Python - Numpy calculating the divisible whole numbers and remainder Python 找出所有能被5整除的奇数 - Finding all odd numbers divisible by 5 如何编写打印三个数字中最大数字的 function - How to write a function that prints the highest number out of three numbers 编写一个输入正整数n并返回可除以17的n位正整数的数量的函数 - Write a function that input a positive integer n and return the number of n-digit positive integers that divisible by 17 查找整数 i 是否可以被列表中的所有元素整除 - find if integer i is divisible by all elements in a list 我需要编写一个仅在两个数字为整数时才打印两个数字之和的代码,否则返回错误? - I need to write a code which prints the sum of two numbers only when they are integer otherwise returns error? 打印范围内可以被 4 或 5 整除的所有数字,但不能同时被 4 或 5 整除 - print all the numbers in a range that are divisible by 4 or 5, but not both 如何创建一个函数以查找可以被7整除但不能被5整除的数字 - How to create a function to find numbers divisible by 7 but not by 5
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM