5 ms·
It seems over-engineered to me. Why go through all the hustle of code generation? It can be solved with a simple “for loop”: func isOdd(n int) bool { var
by bilinguliar 3y ago
It seems over-engineered to me. Why go through all the hustle of code generation? It can be solved with a simple “for loop”:
func isOdd(n int) bool {
var odd bool
for i := 0; i < n; i++ {
odd = !odd
}
return odd
}
Playground link: https://go.dev/play/p/8TIfzGrdWDF https://go.dev/play/p/8TIfzGrdWDF
I did not profile this one yet. But my intuition, and my industry experience, tell me that this is fast.
- javajosh 3y agoYou, sir, are a raging psychopath. You have found a local maxima of brevity and absurdity and I, for one, salute you.
- clarkdale 3y agoYou can improve this with tail recursion.
- YetAnotherNick 3y agoIt will iterate infinitely for n=infinite.
- WJW 3y agoIs infinite even or odd?
- Exoristos 3y agoYes.
- YetAnotherNick 3y agoIdeally it should throw exception I guess.
- KingLancelot 3y ago[dead]
- gpm 3y agoI can confirm that the rust version of this one is fast: playground::isOdd: testq %rdi, %rdi setg %al andb %dil, %al retq (Click ... beside build to get assembly) https://play.rust-lang.org/?version=stable&mode=release&edition=2021&gist=5e7bf681bcfd27ddf9916ebf4e113b1c https://play.rust-lang.org/?version=stable&mode=release&edit... Unfortunately the go playground doesn't seem to support emitting assembly?
- bilinguliar 3y agoShameless plug to claim Rust superiority!1 No, Go playground can not do that :(
- jeroenhd 3y agohttps://godbolt.org/ https://godbolt.org/ supports emitting assembly for Go while Google's tools catch up!
- gpm 3y agoGood point! Neither gc nor gccgo -O seem to figure this function out :( https://godbolt.org/z/eMv41nc6Y https://godbolt.org/z/eMv41nc6Y Proof, that you must use rust if you want blazingly fast execution of fearlessly pessimized code!
- bilinguliar 3y agoThis commands an issue in the Go repo!
- flakes 3y agoA true production quality implementation should always use recursion. func isOdd(n int) bool { switch { case n == 0: return false case n > 0: return !isOdd(n-1) default: return !isOdd(n+1) } }
- make3 3y agothis is truely evil :)
- grishka 3y agoThis will cause a stack overflow. You can convert that into a loop and make your own stack on the heap if you need very deep recursion.
- oldsecondhand 3y agoIn a language with tail-call optimization, it won't.
- dmurray 3y agoIt will. The negations are only being applied on the way back up the call stack. The mutual recursion version in a sibling post can be made to work though.
- ufo 3y agoSurprisingly, some compilers can automatically turn that to a loop too! Try it on clang and gcc :)
- grishka 3y agoGCC does eliminate one of the recursive calls but retains the other: https://godbolt.org/z/dof4T4vYv https://godbolt.org/z/dof4T4vYv But it also does some magic that I don't quite understand (what do the two `sub` instructions do before the `call`? Do they prepare the stack?) because x86 assembly is so confusing to me sometimes.
- scaredginger 3y agoI think this counts as a bottom-up DP solution
- devjam 3y agoDon't forget the companion isEven function: func isEven(n int64) bool { return !isOdd(n) }
- pjerem 3y agoWow you can call a function from a function ? It’ll revolutionize my productivity.
- virgoerns 3y agoDoes Go default-initialize booleans by any chance? In C, which author had used, this program would be undefined behavior due to reading non-static, uninitialized bool.
- bilinguliar 3y agoGo has “zero” values. The zero value of Boolean is false. https://go.dev/tour/basics/12 https://go.dev/tour/basics/12