Header Ad

HackerRank Smart Number problem solution

In this HackerRank Smart Number problem solution, 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.

hackerrank smart number problem solution


Problem solution in Python.

import math

def is_smart_number(num):
    val = int(math.sqrt(num))
    if val ** 2 == num:
        return True
    return False

for _ in range(int(input())):
    num = int(input())
    ans = is_smart_number(num)
    if ans:
        print("YES")
    else:
        print("NO")


Problem solution in C++.

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

bool is_smart_number(int num) {
    int val = (int) sqrt(num);
    if((val*val)==num)
        return true;
    return false;
}

int main() {
    
    int test_cases;
    cin >> test_cases;
    int num;
    for(int i = 0; i < test_cases; i++) {
        cin >> num;
        bool ans = is_smart_number(num);
        if(ans) {
            cout << "YES" << endl;
        }
        else cout << "NO" << endl;
    }
    return 0;
}


Post a Comment

0 Comments