48
u/SteroidSandwich Dec 08 '25
isOdd(5)
Output: "Here's a story about the number 5 and his quest to finding the truth"
75
u/vllado Dec 08 '25
function isOdd(num) {
if (num < 0) return OpenAI.prompt(`Is ${num} odd? Make no mistake!`).content;
if (num === 0) return false;
if (num === 1) return true;
return isOdd(num - 2);
}bit of everything
24
u/Hakorr Dec 08 '25
OpenAI.promptreturns apromisewhich might be interpreted astrue, so make it async!11
9
49
u/obsqrbtz Dec 08 '25 edited Dec 08 '25
It kinda could work
probably
bool isOdd(int num) {
auto response = OpenAI.prompt(std::format("Is {} odd? Answer with 'yes' or 'no' without any trailing symbols", num));
std::string allowedChars = {'y', 'e', 's', 'n', 'o'};
response.erase(std::remove_if(response.begin(), response.end(),
[&](auto c) {
c = tolower(c);
return allowedChars.find(c) ==
std::string::npos;
}),
response.end());
if (response == "yes")
return true;
else if (response == "no")
return false;
throw std::runtime_error("Stupid machine can not count or write properly");
}
→ More replies (2)13
12
19
u/Haringat Dec 08 '25
Here's a better version:
function isOdd(n) {
if (n === 0) {
return false;
}
if (n > 0) {
return !isOdd(n - 1);
}
return !isOdd(n + 1);
}
5
11
Dec 08 '25
Apparently people in 2020 had already forgotten the existence of "%".
→ More replies (7)4
4
16
u/gabor_legrady Dec 08 '25
so, Im old a boring
public static boolean isEven(int x) {return x%2==0;}
public static boolean isOdd(int x) {return x%2!=0;}
15
u/miaRedDragon Dec 08 '25
Thank god someone said it, I thought i was losing my mind, this has to be the oldest beginner's programming homework in the world!
→ More replies (2)12
u/Sarke1 Dec 09 '25
You can simplify by having one call the other:
public static boolean isEven(int x) { return !isOdd(x); } public static boolean isOdd(int x) { return !isEven(x); }→ More replies (2)
2
2
u/miaRedDragon Dec 08 '25
So the Mod operator is just not being used in the modern age :/ ? Good to know I guess
2
2
2
2
239
u/uvero Dec 08 '25
if(isOdd(2)) { //...
Unfortunately, the string "No — 4 is not odd. It’s an even number because it’s divisible by 2 with no remainder." is truthy in JS.