```
int next = 1;
long sum = 0;
int count = 0;
while (sum < n) {
sum += next;
next++;
count++;
}
if (sum == n) {
return count;
} else {
return count - 1;
}
```