I have been given the following homework to solve in python.
You are given one integer number n. Find three distinct integers a,b,c such that 2≤a,b,c, and a⋅b⋅c=n or say that it is impossible to do it.
If there are several answers, you can print any.
You have to answer t independent test cases. The first line of the input contains one integer t (1≤t≤100) — the number of test cases.
The next n lines describe test cases. The i-th test case is given on a new line as one integer n.
Input: 5=t, 64, 32, 97, 2, 12345
Output: YES, 2 4 8 , NO, NO, NO, YES, 3 5 823
I have tried and it works except for the last example output. I am not sure what is wrong and how to solve this. Any help would be appreciated. (I know it would be better to use 'or' but I wanted to make sure I include every condition to see better whats wrong.)
import math
def divisors(t,n):
for k in range(t,0,-1):
a=0
b=0
c=0
k=0
l=n
for i in (2,math.sqrt(n)):
if n%i==0 :
if k==0:
a=i
n =n/a
elif k==1:
b=i
k=k 1
if k==2:
break
if (a==1 or b==1):
print('NO')
break
if a*b==0:
print('NO')
break
if a==b:
print('NO')
break
c=l/(a*b)
if c==b:
print('NO')
break
if a==c:
print('NO')
break
if(c<2):
print('NO')
break
else:
print('YES', int(a ),int(b ), int(c))
break
CodePudding user response:
My attempt:
import numpy as np
def div(cases): #Python can work without indexing (No of cases).
for i in cases:
r = [] #Here, I store all the factors. Is reset every case.
for j in range(2,i 1): #Loop through all the possible divisors.
if i % j == 0: #Check if the case has no remainder with the current factor.
r = [j] #If factor found, add to list
i = i//j #Divide the case by the found factor
if len(r) < 3:
print('NO')
else:
print(f'YES: {[r[0], r[1], np.prod(r[2:])]}') #Print 3 and 3 numbers only
#print(f'YES: {r}')
Input:
div([64, 32, 97, 2, 12345])
Output:
YES: [2, 4, 8]
NO
NO
NO
YES: [3, 5, 823]