# Shortest Solution in C++

• Originally I'd seen "2 line C++ solution" and thought it was cheating, but then I thought: if we're going to cheat, how can we make this the shortest possible solution?

Let us only count non-whitespace chars.

We started with -- 125 chars:

``````class Solution {
public:
bool checkPerfectNumber(int num) {
static unordered_set<int> n = {6, 28, 496, 8128, 33550336};
return n.count(num);
}
};
``````

An easy change -- 109 chars:

``````class Solution {
public:
bool checkPerfectNumber(int num) {
set<int> n = {6, 28, 496, 8128, 33550336};
return n.count(num);
}
};
``````

Now it strikes me that I should just try a simple return statement and get rid of num: -- 101 chars

``````class Solution {
public:
bool checkPerfectNumber(int n) {
return n==6 || n==28 || n==496 || n == 8128 || n == 33550336;
}
};
``````

Then two simple optimizations occur to me: replace "||" with "|" and replace "bool" with "int" -- 96 chars:

``````class Solution {
public:
int checkPerfectNumber(int n) {
return n==6 | n==28 | n==496 | n == 8128 | n == 33550336;
}
};
``````

Finally,I think well why are we even using classes if we don't need to -- 90 chars:

``````struct Solution {
int checkPerfectNumber(int n) {
return n==6 | n==28 | n==496 | n == 8128 | n == 33550336;
}
};
``````

Thanks, for reading! Any suggestions welcome.

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.