简体   繁体   English

所有可能的因素对

[英]All possible pairs of factors

I am trying to write a program that takes a number with a single leading integer and an arbitrary number of trailing zeros and then prints all possible combinations of two factors. 我正在尝试编写一个程序,该程序采用带有单个前导整数和任意数量的尾随零的数字,然后打印两个因子的所有可能组合。

ie. 即。

100 100

factors are 2^2, 5^2 因子是2 ^ 2,5 ^ 2

so the program would print: 所以该程序将打印:

(2,50),(4,25),(5,20) (2,50),(4,25),(5,20)

or 要么

600 600

factors are 2^3,3,5^2 因素是2 ^ 3,3,5 ^ 2

(2,300),(4,150),(8,75),(3,200),(5,120),(25,24),(6,100),(12,50),(15,40),(30,20),(60,10) (2300),(4150),(8.75),(3200),(5120),(25,24),(6100),(12,50),(15,40),(30,20), (60,10)

...I think that's all of them? ......我想这就是全部吗? Is it? 是吗? I could use a program to check... 我可以使用程序检查......

import itertools

facts=[[2,2,2],[3],[5,5]]
for n in itertools.product(*facts)
    print(n)

I see that I am using this incorrectly but that was my first stab at it. 我看到我使用的不正确但这是我第一次尝试它。

This just gives (2,3,5) ten times. 这只给了(2,3,5)十次。

I want something like (2) * (2,3,5,5) and (2,2) * (3,5.5) and the like... 我想要像(2)*(2,3,5,5)和(2,2)*(3,5.5)之类的东西......

Here's how I would do it: 我是这样做的:

def factors(n):
  # Fill this in

def factor_pairs(n):
  for i in factors(n):  # You need to write the factor() function
    yield i, n / i

if __name__ == '__main__':
  n = input('Enter an integer: ')

  for i, j in factor_pairs(n):
    print i, j

I'm not going to code it for you entirely, but you get the idea. 我不会完全为你编码,但你明白了。

To generate all factors of a number given its prime factors : 根据其主要因素生成一个数字的所有因子

#!/usr/bin/env python
import itertools, operator

def all_factors(prime_dict):
    series = [[p**e for e in range(maxe+1)] for p, maxe in prime_dict.items()]
    for multipliers in itertools.product(*series):
        yield reduce(operator.mul, multipliers)

Example

prime_dict = {2:3, 3:1, 5:2}
L = sorted(all_factors(prime_dict))
number_of_divisors = reduce(lambda prod, e: prod*(e+1), prime_dict.values(),1)
assert len(L) == number_of_divisors
# -> [1, 2, 3, 4, 5, 6, 8, 10, 12, 15, 20, 24,
#     25, 30, 40, 50, 60, 75, 100, 120, 150, 200, 300, 600]

To produce pairs: 生产对:

n, isodd = divmod(len(L), 2)
print(zip(L[:n], reversed(L[n + isodd:])))
if isodd: # number is perfect square
   print((L[n], L[n]))

Output 产量

[(1, 600), (2, 300), (3, 200), (4, 150), (5, 120), (6, 100),
 (8, 75), (10, 60), (12, 50), (15, 40), (20, 30), (24, 25)]

It works for small numbers. 它适用于少数。 You could use it to test your solution that could take into account the special form of your numbers: x00000... 您可以使用它来测试您的解决方案,该解决方案可以考虑您的数字的特殊形式: x00000...

I think this would do what you want: 我想这会做你想要的:

n = input('Enter a number? ')
factors = []

for i in range(int(sqrt(n))):
  if n % i == 0:
    factors.append((i, n / i))

By the definition of factors, the maximum number you have to check is up to the square root of the number, so if you can code this, you should be set. 根据因子的定义,您必须检查的最大数量取决于数字的平方根,因此如果您可以对此进行编码,则应该设置。

If you can code this, you should be set. 如果你可以编码,你应该设置。

You can put it all in a list comprehension 你可以把它全部放在列表理解中

import math
n = 600 # or whatever...
[(x, n/x) for x in range(1, int(math.sqrt(n))+1) if n % x == 0]

Returns: 返回:

[(1, 600), (2, 300), (3, 200), (4, 150), (5, 120), (6, 100), (8, 75), (10, 60), (12, 50), (15, 40), (20, 30), (24, 25)]

If you don't want (1,600) just use range(2, int(math.sqrt(n))+1). 如果你不想要(1,600)只使用范围(2,int(math.sqrt(n))+ 1)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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