In this Smart Number 2 HackerRank solution, our challenge task is to debug the existing code to successfully execute all provided test files.
A number is called a smart number if it has an odd number of factors. Given some numbers, find whether they are smart numbers or not.
Debug the given function is_smart_number
to correctly check if a given number is a smart number.
Note: You can modify only one line in the given code and you cannot add or remove any new lines.
To restore the original code, click on the icon to the right of the language selector.
Input Format
The first line of the input contains , the number of test cases.
The next lines contain one integer each.
Constraints
- , where is the integer.
Output Format
The output should consist of lines. In the line print YES if the integer has an odd number of factors, else print NO.
Sample Input
4
1
2
7
169
Sample Output
YES
NO
NO
YES
Explanation
The factors of 1 are just 1 itself.So the answer is YES. The factors of 2 are 1 and 2.It has even number of factors.The answer is NO. The factors of 7 are 1 and 7.It has even number of factors.The answer is NO. The factors of 169 are 1,13 and 169.It has odd number of factors.The answer is YES.
Smart Number 2 HackerRank Solution
I will Provide solution in Multiple programming languages for you. If you are not able to find the code in required language then please share in comments so that our team can help you.
Problem Solution in Python
def is_smart_number(num):
val = int(math.sqrt(num))
if num / val == val:
return True
return False
Problem Solution in JavaScript
function processData(input) {
//Enter your code here
const arr = input.split('\n').slice(1)
arr.forEach(num => {
let count = 0
parsed = parseInt(num)
for(i=1; i <= parsed; i++){
if(parsed % i === 0) {
count ++
}
}
if(count % 2 === 0){
console.log("NO")
} else {
console.log("YES")
}
count = 0
} )
}
Problem Solution in Java
public static boolean isSmartNumber(int num) {
int val = (int) Math.sqrt(num);
if((float)num / val == val)
return true;
return false;
}
Leave a Reply