Actually that shows "Time Limit Exceeded" A better solution can be: if (n < 0) return false; while (n > 1) { if (n % 2 == 0) n /= 2; else if (n % 3 == 0) n /= 3; else if (n % 5 == 0) n /= 5; else return false; } return n ==1;
1 has no prime factors, therefore all of its prime factors are limited to 2,3 and 5..so we can take it as ugly number.. Hence return true for this case..
20 is not considered an ugly number. An ugly number must only be divisible by 2, 3, or 5, but 20 is also divisible by 4, which is not among the allowed prime factors. -source chatgpt
Being divisible by 4 is the same as being divisible by 2 two times... I'm not sure if Chad GPT is going to do mentally cripple an entire generation of engineers, or help them learn quicker through failure
there are times that i wonder if im being punished for skipping all my math classes during undergrad
I feel the same sometimes lol
There are times I wonder if I am being punished for not studying Computer Science Major but trying to be a software programmer.. lol
My Favorite YT chanel, don't ask me why
why
@@ambujhakhu7531 dont worry why he said
But how
you can also add one more condition at while loop that n!= 0 otherwise the loop will be infinite.
You just made it quick and easy.. Thanks!!
Glad it helped!
i think the worse case time complexity is O(log(2)n) that's if n is only divisible by 2.
I think the time complexity could be O(1).
I watch entire videos! Because I'm a beginner so the coding section is really helpful! Thank you for the great explanation :)
the GOAT uploaded
As aways!
hi - is there a leetcode playlist for this as well?
Actually that shows "Time Limit Exceeded"
A better solution can be:
if (n < 0)
return false;
while (n > 1)
{
if (n % 2 == 0) n /= 2;
else if (n % 3 == 0) n /= 3;
else if (n % 5 == 0) n /= 5;
else return false;
}
return n ==1;
I came up with this exact solution without seeing this code.
Great explanation
What happens if n is 1. This code seems like it return it as True even though the prime number of 1 is not 2 3 or 5. shouldn't it be if n
if n == 0: return False
while n % 2 == 0:
n = n / 2
while n % 3 == 0:
n = n / 3
while n % 5 == 0:
n = n / 5
return n == 1
+1
1 has no prime factors, therefore all of its prime factors are limited to 2,3 and 5..so we can take it as ugly number..
Hence return true for this case..
20 is not considered an ugly number. An ugly number must only be divisible by 2, 3, or 5, but 20 is also divisible by 4, which is not among the allowed prime factors. -source chatgpt
4 is not prime
Being divisible by 4 is the same as being divisible by 2 two times...
I'm not sure if Chad GPT is going to do mentally cripple an entire generation of engineers, or help them learn quicker through failure
i think u r from mars ur every video is educative
Thanks
but the test case says 20 is an ugly number.
Helpfull
++subscribers