-6

So I've looked everywhere and still can't figure it out.

Basically what I want to do is split number x into two integers that when multiplied they give as result x.

For example:
Input: 10
Output: 5, 2

Is there a way to do this in python? Thanks in advance.

T. Kropalis
  • 334
  • 2
  • 6
  • 1
    What do you mean with compare them to the number `c`. Please provide *input* and *expected output*. Show what you've *tried*,... – Willem Van Onsem Feb 20 '17 at 18:38
  • 3
    You can split `x` into `x, 1` ;) – kennytm Feb 20 '17 at 18:38
  • 1
    how would you split the number `123` *into two integers* ? – RomanPerekhrest Feb 20 '17 at 18:40
  • Just realised the last part with number `c` wasn't necessary. I'm going to make the required changes with input and expected output. Also, why the downvotes? – T. Kropalis Feb 20 '17 at 18:44
  • @T.Kropalis: didn't dv. But usually people expect that you put *effort* in a question *before* posting it. [Jon Skeet's checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/) actually provides a good way to ask a question. – Willem Van Onsem Feb 20 '17 at 18:48
  • 1
    The term you're looking for is [factorization](https://www.khanacademy.org/math/in-eighth-grade-math/factorization) - Khan Academy – TessellatingHeckler Feb 20 '17 at 18:54

2 Answers2

2

With this you can find all of the possible combinations, including (1, x):

import math  # Needed to generate the best range, so you have no repeated combinations.
possible_combinations = [(i, x / i) for i in range(1, int(math.ceil(x**0.5)) + 1) if x % i == 0]
for item in possible_combinations:
    print item
francisco sollima
  • 7,952
  • 4
  • 22
  • 38
1

Every integer is divisible by itself and by 1. If the integer is composite, it will have at least one other pair of divisors (which may be the same, as in the case of 4, which is divisible by 1,4 and 2,2).

lim = int(math.sqrt(num))
for i in range (1, lim):
    if num%i==0:
        print(i, int(num/i))
Jake Morris
  • 645
  • 6
  • 18
  • If `num=4` this will only output `1 4` and omit `2 2` because you do `range(1, lim)` which ends at `lim-1`. You should change it to `range(1, lim+1)`. If someone is looking for prime factors they'll want to use `range(2, lim+1)`. – Steven Rumbalski Sep 11 '18 at 14:26