Find two proper factors of N such that their sum is coprime with N
Given an integer N, you have to find two proper factors of N such that their sum is coprime with the given integer N. If no such factors exist, print -1.
Examples:
Input: N = 15
Output: 3, 5
Explanation: 3 and 5 are the proper factors of 15 and 3+5 -> 8 is coprime with 15.Input: N = 4
Output: -1
Explanation: there are no proper factors that satisfy the required conditions
Naive Approach: Generate a list of all the proper factors of N and for each possible pair, check if their sum is coprime with N i.e. GCD(sum of pair of integers, N) = 1. Here GCD means Greatest Common Divisor.
Efficient Approach: If two numbers A and B are coprime then their sum is coprime with their product. Keeping that in mind, find all the factors of N and for each factor d1, calculate the largest factor of N, d2 that is coprime with d1. To calculate d2, simply divide N with d1 until N%d1 != 0. Finally, check if d1 and d2 are proper factors of N or not (i.e., d1>1 and d2>1).
Below is the implementation of the above approach:
C++
// C++ Program for the above approach #include <bits/stdc++.h> using namespace std; // Function to find two proper // factors of N such that their // sum is coprime with N void printFactors( int n) { // Find factors in sorted order for ( int i = 2; i <= sqrt (n); i++) { if (n % i == 0) { int d1 = i, d2 = n; // Find largest value of d2 such // that d1 and d2 are co-prime while (d2 % d1 == 0) { d2 = d2 / d1; } // Check if d1 and d2 are proper // factors of N if (d1 > 1 && d2 > 1) { // Print answer cout << d1 << ", " << d2; return ; } } } // No such factors exist cout << -1; } // Driver code int main() { int N = 10; // Function Call printFactors(N); return 0; } |
Java
// Java program for the above approach import java.io.*; class GFG{ // Function to find two proper // factors of N such that their // sum is coprime with N static void printFactors( int n) { // Find factors in sorted order for ( int i = 2 ; i <= ( int )Math.sqrt(n); i++) { if (n % i == 0 ) { int d1 = i, d2 = n; // Find largest value of d2 such // that d1 and d2 are co-prime while (d2 % d1 == 0 ) { d2 = d2 / d1; } // Check if d1 and d2 are proper // factors of N if (d1 > 1 && d2 > 1 ) { // Print answer System.out.print(d1 + ", " + d2); return ; } } } // No such factors exist System.out.print(- 1 ); } // Driver code public static void main(String[] args) { int N = 10 ; // Function Call printFactors(N); } } // This code is contributed by Potta Lokesh |
Python3
# Python Program for the above approach import math # Function to find two proper # factors of N such that their # sum is coprime with N def printFactors(n): # Find factors in sorted order for i in range ( 2 , int (math.sqrt(n)) + 1 ): if (n % i = = 0 ): d1 = i d2 = n # Find largest value of d2 such # that d1 and d2 are co-prime while (d2 % d1 = = 0 ): d2 = d2 / / d1 # Check if d1 and d2 are proper # factors of N if (d1 > 1 and d2 > 1 ): # Print answer print (d1, d2, sep = ", " ) return # No such factors exist print ( - 1 ) # Driver code N = 10 # Function Call printFactors(N) # This code is contributed by Shivani |
C#
// C# Program for the above approach using System; using System.Collections.Generic; class GFG{ // Function to find two proper // factors of N such that their // sum is coprime with N static void printFactors( int n) { // Find factors in sorted order for ( int i = 2; i <= ( int )Math.Sqrt(n); i++) { if (n % i == 0) { int d1 = i, d2 = n; // Find largest value of d2 such // that d1 and d2 are co-prime while (d2 % d1 == 0) { d2 = d2 / d1; } // Check if d1 and d2 are proper // factors of N if (d1 > 1 && d2 > 1) { // Print answer Console.Write(d1 + ", " +d2); return ; } } } // No such factors exist Console.Write(-1); } // Driver code public static void Main() { int N = 10; // Function Call printFactors(N); } } // This code is contributed by ipg2016107. |
Javascript
<script> // Javascript Program for the above approach // Function to find two proper // factors of N such that their // sum is coprime with N function printFactors(n) { // Find factors in sorted order for (let i = 2; i <= Math.sqrt(n); i++) { if (n % i == 0) { let d1 = i, d2 = n; // Find largest value of d2 such // that d1 and d2 are co-prime while (d2 % d1 == 0) { d2 = Math.floor(d2 / d1); } // Check if d1 and d2 are proper // factors of N if (d1 > 1 && d2 > 1) { // Print answer document.write(d1 + ", " + d2); return ; } } } // No such factors exist document.write(-1); } // Driver code let N = 10; // Function Call printFactors(N); // This code is contributed by _saurabh_jaiswal. </script> |
2, 5
Time Complexity: O(√N)
Auxiliary Space: O(1)
Please Login to comment...