- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Minimum number of squares whose sum equals to given number nn
Any numbers can be represented by the sum of some perfect square numbers. In this problem, we need to find that how many minimum numbers of perfect square terms are needed to represent the given value.
let the value is 94,so 95 = 92 + 32 + 22 + 12. so the answer will be 4
The idea is to start from 1, we move further to get perfect squared numbers. When the value is 1 to 3, they must be formed with only 1s.
Input and Output
Input: An integer number. Say 63. Output: Number of squared terms. Here the answer is 4. 63 =72 + 32 + 22 + 1
Algorithm
minSquareTerms(value)
Input: The given value.
Output: Minimum number of square terms to reach given value.
Begin define array sqList of size value + 1 sqList[0] := 0, sqList[1] := 1, sqList[2] := 2, sqList[3] := 3 for i in range 4 to n, do sqList[i] := i for x := 1 to i, do temp := x^2 if temp > i, then break the loop else sqList[i] := minimum of sqList[i] and (1+sqList[i-temp]) done done return sqList[n] End
Example
#include<bits/stdc++.h> using namespace std; int min(int x, int y) { return (x < y)? x: y; } int minSquareTerms(int n) { int *squareList = new int[n+1]; //for 0 to 3, there are all 1^2 needed to represent squareList[0] = 0; squareList[1] = 1; squareList[2] = 2; squareList[3] = 3; for (int i = 4; i <= n; i++) { squareList[i] = i; //initially store the maximum value as i for (int x = 1; x <= i; x++) { int temp = x*x; //find a square term, lower than the number i if (temp > i) break; else squareList[i] = min(squareList[i], 1+squareList[itemp]); } } return squareList[n]; } int main() { int n; cout << "Enter a number: "; cin >> n; cout << "Minimum Squared Term needed: " << minSquareTerms(n); return 0; }
Output
Enter a number: 63 Minimum Squared Term needed: 4
- Related Articles
- Minimum number of squares whose sum equals to given number n\n
- Count of n digit numbers whose sum of digits equals to given sum in C++
- Print all n-digit numbers whose sum of digits equals to given sum in C++
- Minimum number of single digit primes required whose sum is equal to N in C++
- Find smallest number n such that n XOR n+1 equals to given k in C++
- Convert a number m to n using minimum number of given operations in C++
- Minimum number of power terms with sum equal to n using C++.
- n-th number whose sum of digits is ten in C++
- Program to count number of consecutive lists whose sum is n in C++
- Minimum number of palindromes required to express N as a sum using C++.
- Find M-th number whose repeated sum of digits of a number is N in C++
- Count pairs (a, b) whose sum of squares is N (a^2 + b^2 = N) in C++
- Minimum number of given moves required to make N divisible by 25 using C++.
- First triangular number whose number of divisors exceeds N in C++
- Smallest number of perfect squares that sums up to n in JavaScript
- C++ program to count minimum number of operations needed to make number n to 1

Advertisements