

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find minimum number to be divided to make a number a perfect square in C++
Suppose we have a number N. We have to find minimum number that divide N to make it perfect square. So if N = 50, then minimum number is 2, as 50 / 2 = 25, and 25 is a perfect square.
A number is perfect square if it has even number of different factors. So we will try to find the prime factors of N, and find each prime factor power. Find and multiply all prime factors whose power is odd.
Example
#include<iostream> #include<cmath> using namespace std; int findMinimumNumberToDivide(int n) { int prime_factor_count = 0, min_divisor = 1; while (n%2 == 0) { prime_factor_count++; n /= 2; } if (prime_factor_count %2) min_divisor *= 2; for (int i = 3; i <= sqrt(n); i += 2) { prime_factor_count = 0; while (n%i == 0) { prime_factor_count++; n /= i; } if (prime_factor_count%2) min_divisor *= i; } if (n > 2) min_divisor *= n; return min_divisor; } int main() { int n = 108; cout << "Minimum number to divide is: " << findMinimumNumberToDivide(n) << endl; }
Output
Minimum number to divide is: 3
- Related Questions & Answers
- 8086 program to find the square root of a perfect square root number
- Find the Next perfect square greater than a given number in C++
- Check if a number is perfect square without finding square root in C++
- Program to find minimum number of characters to be added to make it palindrome in Python
- Python Program to Check if a Number is a Perfect Number
- Golang Program to Check if a Number is a Perfect Number
- Java Program to Find The Perfect Number
- PHP program to find the first natural number whose factorial can be divided by a number ‘x’
- Program to find probability that any proper divisor of n would be an even perfect square number in Python
- Check if given number is perfect square in Python
- 8085 program to find square root of a number
- 8086 program to find Square Root of a number
- Minimum number of Parentheses to be added to make it valid in C++
- Check whether the number can be made perfect square after adding 1 in Python
- Program to find minimum number of operations required to make one number to another in Python
Advertisements