Find all Factors of Large Perfect Square Natural Number in O ( sqrt ( sqrt ( N ) ) | Python 3 program to find the factors of large perfect square number in O ( sqrt ( sqrt ( N ) ) ) time ; Function that find all the prime factors of N ; Store the sqrt ( N ) in temp ; Initialise factor array with 1 as a factor in it ; Check divisibility by 2 ; Store the factors twice ; Check for other prime factors other than 2 ; If j is a prime factor ; Store the prime factor twice ; If j is prime number left other than 2 ; Store j twice ; Initialise Matrix M to to store all the factors ; tpc for rows tpr for column ; Initialise M [ 0 ] [ 0 ] = 1 as it also factor of N ; Traversing factor array ; If current and previous factors are not same then move to next row and insert the current factor ; If current and previous factors are same then , Insert the factor with previous factor inserted in matrix M ; The arr1 [ ] and arr2 [ ] used to store all the factors of N ; Initialise arrays as 1 ; Traversing the matrix M print ( " tpr ▁ " , tpr ) ; Traversing till column element doesn 't become 0 ; Store the product of every element of current row with every element in arr1 [ ] ; Copying every element of arr2 [ ] in arr1 [ ] ; length of arr2 [ ] and arr1 [ ] are equal after copying ; Print all the factors ; Drivers Code
import math NEW_LINE MAX = 100000 NEW_LINE def findFactors ( N ) : NEW_LINE INDENT temp = int ( math . sqrt ( N ) ) NEW_LINE factor = [ 1 ] * MAX NEW_LINE len1 = 1 NEW_LINE while ( temp % 2 == 0 ) : NEW_LINE INDENT factor [ len1 ] = 2 NEW_LINE len1 += 1 NEW_LINE factor [ len1 ] = 2 NEW_LINE len1 += 1 NEW_LINE temp //= 2 NEW_LINE DEDENT sqt = math . sqrt ( temp ) NEW_LINE for j in range ( 3 , math . ceil ( sqt ) , 2 ) : NEW_LINE INDENT while ( temp % j == 0 ) : NEW_LINE INDENT factor [ len1 ] = j NEW_LINE len1 += 1 NEW_LINE factor [ len1 ] = j NEW_LINE len1 += 1 NEW_LINE temp //= j NEW_LINE DEDENT DEDENT if ( temp > 2 ) : NEW_LINE INDENT factor [ len1 ] = temp NEW_LINE len1 += 1 NEW_LINE factor [ len1 ] = temp NEW_LINE len1 += 1 NEW_LINE DEDENT M = [ [ 0 for x in range ( MAX ) ] for y in range ( len1 ) ] NEW_LINE tpc , tpr = 0 , 0 NEW_LINE M [ 0 ] [ 0 ] = 1 NEW_LINE j = 1 NEW_LINE while ( j < len1 ) : NEW_LINE INDENT if ( factor [ j ] != factor [ j - 1 ] ) : NEW_LINE INDENT tpr += 1 NEW_LINE M [ tpr ] [ 0 ] = factor [ j ] NEW_LINE j += 1 NEW_LINE tpc = 1 NEW_LINE DEDENT else : NEW_LINE INDENT M [ tpr ] [ tpc ] = M [ tpr ] [ tpc - 1 ] * factor [ j ] NEW_LINE j += 1 NEW_LINE tpc += 1 NEW_LINE DEDENT DEDENT arr1 = [ 0 ] * MAX NEW_LINE arr2 = [ 0 ] * MAX NEW_LINE l1 = l2 = 1 NEW_LINE arr1 [ 0 ] = 1 NEW_LINE arr2 [ 0 ] = 1 NEW_LINE for i in range ( 1 , tpr + 1 ) : NEW_LINE INDENT j = 0 NEW_LINE while M [ i ] [ j ] != 0 : NEW_LINE INDENT for k in range ( l1 ) : NEW_LINE INDENT arr2 [ l2 ] = arr1 [ k ] * M [ i ] [ j ] NEW_LINE l2 += 1 NEW_LINE DEDENT j += 1 NEW_LINE DEDENT for j in range ( l1 , l2 ) : NEW_LINE INDENT arr1 [ j ] = arr2 [ j ] NEW_LINE DEDENT l1 = l2 NEW_LINE DEDENT for i in range ( l2 ) : NEW_LINE INDENT print ( arr2 [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 900 NEW_LINE findFactors ( N ) NEW_LINE DEDENT