Given some numbers, we need to find whether they are smart numbers or not. If they are smart we should print “YES” otherwise print “NO”.
A smart number is defined as a number that has an odd number of factors.
We have to perform debugging of the code. We can only modify one line of code and can not add or delete any line.
import math def is_smart_number(num): val = int(math.sqrt(num)) if num / val == 1: return True return False for _ in range(int(input())): num = int(input()) ans = is_smart_number(num) if ans: print("YES") else: print("NO")
We can only modify one line in this code.
Approach for Smart Number Hackerrank Solution
The different factors of a number form pairs among each other. If x is a factor of a number n, we know that n/x will also be a factor of that number. For example, 2 is a factor of 10, then 10/2, 5 is also a factor of 10.
This is the approach that we use to find factors of a number in O(sqrt(n)) time complexity. We just loop through all numbers till sqrt(n). If it is a factor we add that number and also n divided by that number to the list of factors. So we always get a pair of factors.
The only time we can have an odd number of factors is if x = n/x. Thus the factor is its own pair. This equation can be simplified as n = x^2 Or x = sqrt(n).
So, if an integer square root of n exists that is if n is a perfect square, we get an odd number of factors, making it a special number.
Thus, we can update the condition to check for this.
if num / val == 1:
if num / val == val:
Smart Number Hackerrank Solution
import math def is_smart_number(num): val = int(math.sqrt(num)) if num / val == val: return True return False for _ in range(int(input())): num = int(input()) ans = is_smart_number(num) if ans: print("YES") else: print("NO")
- Item Price Generator Hackerrank Solution
- Insertion Sort Part 2 Hackerrank Solution in C
- Extra Long Factorials Hackerrank Solution
- Java Datatypes Hackerrank Solution
- Smart Number Hackerrank Solution
- Python Average Function Hackerrank Solution
- Company Logo Hackerrank Solution
- Matrix Summation Hackerrank Solution
- A company is repainting its office – HackerRank Solution
- Maximum Product Subarray in O(N) Time – Leetcode Solution
- Minimum number of transformation steps required to make all elements of an array equal
- NxNxN Matrix in Python 3
- Boruvka’s Algorithm in Python
- Roti Prata SPOJ Problem Solution – Complete Solution Approach with C++ and Java Code
- Lee Algorithm in Python | Solution to Maze Routing Problem in Python
- Vending Machine with Python Code
- Top 20 Array Coding Questions for Online Assessment
- Heap Sort Algorithm in Python
- Heap Sort Algorithm|Heap Data Structure
- Insertion Sort Algorithm in Data Structures using Python
- Quick Sort algorithm in data structures and algorithms using Python
- Selection Sort Algorithm In Data Structures and Algorithms using Python
- Searching Algorithms: Linear Search Algorithm
- Bubble Sort Algorithm In Data Structures & Algorithms using Python
- Merge Sort Algorithm in Python
- Binary Search in python
- Linear Search Python
- Sorting Algorithms and Searching Algorithms in Python
- Binary Search In Python
- Adding three matrices