Find values of P and Q satisfying the equation N = P^2.Q
Given a number N (1 ? N ? 9×1018), the task is to find P and Q satisfying the equation N = P 2. Q where P and Q will be prime numbers.
Examples:
Input: N = 175
Output: P = 5, Q = 7
Input: N = 2023
Output: P = 17, Q = 7
Method 1:
Approach: The problem can be solved based on the following idea:
As N = P2. Q then min(P, Q) ? 3?N .So we can find at least one of P and Q by doing iterations up to 3?N.
Follow the below steps to implement the idea:
- Let M be the maximum integer i among i = 1, 2, …, [ 3?N] that divides N (actually, M=P or Q).
- Check N/M divided by M . If it is divisible, P = M and Q = N/M2; otherwise, P = ?(N/M) and Q = M2.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void solvePQ( int n)
{
long long int p = 0, q = 0;
for ( long long int i = 2; (i * i * i) <= n; i++) {
if (n % i != 0)
continue ;
if ((n / i) % i == 0) {
p = i;
q = n / (i * i);
}
else {
q = i;
p = ( long long int )round( sqrt (n / i));
}
}
cout << "P is " << p << "\n"
<< "Q is " << q << endl;
}
int main()
{
long long int N = 2023;
solvePQ(N);
return 0;
}
|
Java
import java.math.BigDecimal;
import java.math.RoundingMode;
public class GFG
{
public static void solvePQ( int n)
{
long p = 0 , q = 0 ;
for ( long i = 2 ; (i * i * i) <= n; i++) {
if (n % i != 0 )
continue ;
if ((n / i) % i == 0 ) {
p = i;
q = n / (i * i);
}
else {
q = i;
p = ( long )Math.round(Math.sqrt(n / i));
}
}
System.out.println( "P is " + p);
System.out.println( "Q is " + q);
}
public static void main(String[] args)
{
int N = 2023 ;
solvePQ(N);
}
}
|
Python3
import math
def solvePQ(n):
p = 0
q = 0
for i in range ( 2 , int (math. pow (n, 1 / 3 )) + 1 ):
if n % i ! = 0 :
continue
if (n / i) % i = = 0 :
p = i
q = n / (i * i)
else :
q = i
p = int (math.sqrt(n / i))
print (p, q)
N = 2023
solvePQ(N)
|
C#
using System;
using System.Linq;
using System.Collections.Generic;
class GFG
{
static void solvePQ( long n)
{
long p = 0, q = 0;
for ( long i = 2; (i * i * i) <= n; i++) {
if (n % i != 0)
continue ;
if ((n / i) % i == 0) {
p = i;
q = n / (i * i);
}
else {
q = i;
p = ( long )(Math.Sqrt(n / i));
}
}
Console.Write( "P is " + p + "\n"
+ "Q is " + q);
}
static public void Main()
{
long N = 2023;
solvePQ(N);
}
}
|
Javascript
function solvePQ(n)
{
let p = 0;
let q = 0;
for (let i = 2; i * i * i <= n; i++)
{
if (n % i !== 0) {
continue ;
}
if ((n / i) % i === 0) {
p = i;
q = n / (i * i);
}
else {
q = i;
p = Math.round(Math.sqrt(n / i));
}
}
console.log( "P is " + p);
console.log( "Q is " + q);
}
solvePQ(2023);
|
Time Complexity: O(3?n)
Auxiliary space: O(1)
Method 2:
Approach:
- Define a function isPrime that takes an integer n and returns a boolean indicating whether n is prime or not.
- If n is less than 2, return false.
- For each integer i from 2 up to the square root of n, do:
- If n is divisible by i, return false.
- Return true.
- Define a function findPQ that takes an integer N and two integer references P and Q.
- Create an empty vector primes.
- For each integer i from 2 up to the square root of N, do:
- If i is prime (i.e., isPrime(i) returns true), add i to the primes vector.
- For each integer p in the primes vector, do:
- If N is divisible by p * p and (N / (p * p)) is prime, do:
- Set P = p and Q = N / (p * p).
- Return from the function.
- If no such P and Q are found, set P and Q to 0 (or any other value to indicate failure).
- Call the findPQ function with the input value of N and references to P and Q.
- Output the resulting values of P and Q.
Below is the implementation of the above approach:
C++
#include <iostream>
#include <vector>
using namespace std;
bool isPrime( int n)
{
if (n < 2)
return false ;
for ( int i = 2; i * i <= n; i++) {
if (n % i == 0)
return false ;
}
return true ;
}
void findPQ( int N, int & P, int & Q)
{
vector< int > primes;
for ( int i = 2; i * i <= N; i++) {
if (isPrime(i))
primes.push_back(i);
}
for ( int p : primes) {
if (N % (p * p) == 0 && isPrime(N / (p * p))) {
P = p;
Q = N / (p * p);
return ;
}
}
}
int main()
{
int N = 175;
int P, Q;
findPQ(N, P, Q);
cout << "P = " << P << ", Q = " << Q << endl;
return 0;
}
|
Java
import java.util.*;
public class Main {
static boolean isPrime( int n)
{
if (n < 2 )
return false ;
for ( int i = 2 ; i * i <= n; i++) {
if (n % i == 0 )
return false ;
}
return true ;
}
static void findPQ( int N, int [] pq)
{
ArrayList<Integer> primes = new ArrayList<>();
for ( int i = 2 ; i * i <= N; i++) {
if (isPrime(i))
primes.add(i);
}
for ( int p : primes) {
if (N % (p * p) == 0 && isPrime(N / (p * p))) {
pq[ 0 ] = p;
pq[ 1 ] = N / (p * p);
return ;
}
}
}
public static void main(String[] args)
{
int N = 175 ;
int [] pq = new int [ 2 ];
findPQ(N, pq);
System.out.println( "P = " + pq[ 0 ] + ", Q = " + pq[ 1 ]);
}
}
|
Python3
import math
def isPrime(n):
if (n < 2 ):
return False
for i in range ( 2 , int (math.sqrt(n)) + 1 ):
if (n % i = = 0 ):
return False
return True
def findPQ(N):
P, Q = None , None
primes = []
for i in range ( 2 , int (math.sqrt(N)) + 1 ):
if (isPrime(i)):
primes.append(i)
for p in primes:
if (N % (p * p) = = 0 and isPrime(N / / (p * p))):
P = p
Q = N / / (p * p)
break
return P, Q
N = 175
P, Q = findPQ(N)
print (f "P = {P}, Q = {Q}" )
|
C#
using System;
using System.Collections.Generic;
public class MainClass {
static bool IsPrime( int n)
{
if (n < 2)
return false ;
for ( int i = 2; i * i <= n; i++) {
if (n % i == 0)
return false ;
}
return true ;
}
static void FindPQ( int N, int [] pq)
{
List< int > primes = new List< int >();
for ( int i = 2; i * i <= N; i++) {
if (IsPrime(i))
primes.Add(i);
}
foreach ( int p in primes)
{
if (N % (p * p) == 0 && IsPrime(N / (p * p))) {
pq[0] = p;
pq[1] = N / (p * p);
return ;
}
}
}
public static void Main( string [] args)
{
int N = 175;
int [] pq = new int [2];
FindPQ(N, pq);
Console.WriteLine( "P = " + pq[0]
+ ", Q = " + pq[1]);
}
}
|
Javascript
function isPrime(n) {
if (n < 2) return false ;
for (let i = 2; i * i <= n; i++) {
if (n % i === 0) return false ;
}
return true ;
}
function findPQ(N) {
const primes = [];
for (let i = 2; i * i <= N; i++) {
if (isPrime(i)) primes.push(i);
}
for (const p of primes) {
if (N % (p * p) === 0 && isPrime(N / (p * p))) {
return [p, N / (p * p)];
}
}
return null ;
}
const N = 175;
const [P, Q] = findPQ(N);
if (P !== undefined && Q !== undefined) {
console.log(`P = ${P}, Q = ${Q}`);
} else {
console.log( "No valid P and Q found." );
}
|
Time Complexity: O(n*logn)
Auxiliary Space: O(n)
Last Updated :
24 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...