54
22
398
407
u/stackOverfloater Aug 13 '25
21k test cases!!? Who is making these?
100
u/In_The_Wild_ <1012> <342> <563> <107> Aug 13 '25
You can a write a loop to generate the test cases for today's problem. For most problems you reverse engineer the solution and generate test cases.
7
112
1
u/DadAndDominant Aug 14 '25
Not sure about leetcode but in pytest, when you parametrize a test, each one counts separatelly. You can have very large number of tests with very few lines of code. Maybe it is same here?
71
32
u/jim-jam-biscuit Aug 13 '25
https://leetcode.com/problems/power-of-three/?envType=daily-question&envId=2025-08-13
this is problm of the day .
my attmpt
class Solution {
public:
bool isPowerOfThree(double n) {
if( n ==1 )
return true ;
else if ( n< 1)
return false ; // main gaurding logic or base condition
return isPowerOfThree( n/3.0);//3.0 nuance was very much needed instead of 3 beacue this would fetch us division value less than 1
}
};14
u/dakata98 Aug 13 '25
given that you work with doubles, i wouldn't really recommend using equality operations. Instead, just check the magnituded of the difference due to calculation errors that are caused by working with floating-point numbers. If you make it lower than 1e-10 then the test will pass for sure. Moreover, when you divide a number by 3 and it isn't a whole number, then for sure the output is false.
9
u/jim-jam-biscuit Aug 13 '25
man i had to check what you said , i got to learn that errors tends stack up as we do multiple divisions , and further in some case we might get value of n as 0.99999 , although it is very close to 1 , but not exactly 1 , and equality operators expect exact number . so even though theoretically , we should have got output as true , we might get false as output .
instead we just checked the difference of the numbers if that is difference is smaller that the value , we say that is good enough and both are 1 ,
This was such a good takeaway , thanks mate ✍🏻
3
2
u/zffr Aug 14 '25
There are only 19 possible powers of 3 that are < 231. You could make this much faster by just using a switch statement for each case.
9
10
1
4
3
3
u/ex2uply Aug 13 '25
bool isPowerOfThree(int n) {
return n > 0 && 1162261467 % n == 0;
}
//gpt-solution -> that large number is basically the highest power of 3 int can store.1
2
2
u/Expensive_Routine_49 Aug 13 '25
bool isPowerOfThree(int n) {
if(n<=1) return n==1;
return (3 * (n/3) == n) && isPowerOfThree(n/3);
}My try
1
2
2
1
1
1
1
1
1
u/Coastzs Aug 13 '25
Just did the exact same thing haha. Was it when the test case is 1 when yours failed too?
1
1
1
1
u/heyuhitsyaboi Aug 13 '25
I noticed this question was left as attempted for me so i submitted my last submitted code from 2023
[Wrong Answer 21036/21040 testcases passed]
and that explains why i left it as attempted back then lol
1
1
u/Muted-Imagination-72 Aug 13 '25
class Solution: def isPowerOfThree(self, n: int) -> bool: if n == 1: return True if n < 1: return False return n % 3 == 0 and self.isPowerOfThree(n // 3)
1
1
1
1
1
1
1
u/jacqlin90 Aug 13 '25
class Solution {
public:
bool isPowerOfThree(int n) {
const int N = 1162261467;
return (n > 0) && (N%n == 0);
}
};1
u/Ace0072 Aug 13 '25
class Solution { public boolean isPowerOfThree(int n) { if (n <= 0) return false; while (n % 3 == 0) { n /= 3; } return n == 1; } }
1
1
1
1
u/Axel_Blazer Aug 13 '25
why are they bothering with such high number of cases lol flexing servers maybe
1
1
1
u/Pure_Education1228 Aug 14 '25
Best code..
return 1162261467%n == 0;
Why n is in power of 3 and 3s largest power under the limitation of int data type.. = 1162261467.. hence if modulo of theirs is 0 then it's true else false.
1
1
1
1
1
u/Acceptable_Pear_6802 Aug 16 '25
21k test cases? Dude accidentally wrote a Legacy distributed system trying to solve a fibonacci question
1
0
381
u/Global_Many4693 Aug 13 '25
With all due respect wtf is this?.You solving all 3000 questions at once?