- Prepare
- Algorithms
- Debugging
- Smart Number
- Discussions

# Smart Number

# Smart Number

+ 0 comments this how i solve it

python :

if val*val ==num and num/val == val:

+ 0 comments This is my

**Java 8**solution, feel free to ask me any questions.Explaination:

`After a period of searching and gaining knowledge, a positive integer has at least two factors: 1 and itself. To calculate the number using its factors only, we multiply pairs of factors. A perfect square has a unique property where one pair of factors combines into a single factor, which is the square root of the number. Consequently, perfect squares have an odd number of factors, while other numbers have an even number of factors.`

Solution:

public static boolean isSmartNumber(int num) { int val = (int) Math.sqrt(num); if(val*val == num) return true; return false; }

+ 0 comments Python:

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

+ 0 comments This question got me confused initially and then looking at the wrong posted answers here in this discussion got me more confused.

Let me simplify it.

***Factors of a number simply means by how many ways can two numbers be multiplied together to get the number which we're finding the factor of. *****Examples:****Factor of 64:**1x64=64 2x32=64 4*16=64 8*8=64Hence, there are

**4**ways to get factor of 64 The question wants us to print**NO**as the number of ways to find the factor of 64 is**4**i.e. an EVEN number**Factor of 100:**1x100=100 2x50=100 4x25=100 5x20=100 10x10=100Hence, there are 5 ways to get factor of 100 The question wants us to print

**YES**as the number of ways to find the factor of 100 is**5**i.e. an ODD number**The Trick**: - When you take squareroot of a number and it returns no remainder then it's an ODD factor number - When you take squareroot of a number and it returns a remainder then it's an EVEN factor numberLike: SQRT(100) returns 10.0 so 100 is an ODD factor number SQRT(8) returns 2.82 so 8 is an EVEN factor number

Based on the given code, we're parsing the result of square root to an INTEGER int val = (int) Math.sqrt(num); So we won't get any remainder value in the variable 'val'

Hence, we can apply a logic that if multiplying the result of the squareroot value with itself it should be equal to the original value. If it's equal its an ODD factor number else it's an EVEN factor number.

Answer: if(val * val == num)

+ 0 comments bool is_smart_number(int num) { int val = (int) sqrt(num); if((double)num / val == val) return true; return false; }

Sort 115 Discussions, By:

Please Login in order to post a comment