❔ Making collatz conjecture code more efficient
hello, i recently tried to make a C# program that runs the collatz conjecture on random numbers and saves the conjecture to a .txt file; however: its really inefficient and i was wondering if anyone had any suggestions on how to improve it?
5 Replies
first, please do not use
goto
jump statements, like basically ever
beyond that there's not a whole lot more efficiency to be gained, it's a fairly fixed algorithm with not much wiggle room
you should only really need while(x > 1)
though, but as an optimisation that's frankly irrelevantyea i tried that and when i did it, it just decided "nope, im gonna become an infinite loop"
so i just didnt fix it lol
what should i use instead?
define a method, and use
return
statements to break out where necessary
if you need to create an infinite loop, wrap that method in while(true)
(or with a break-out condition if you want to exit it at any point)
I can't be bothered to implement it myself so you could take some lessons from this implementation I found on StackOverflow
seems to be broadly similar to yours in general thoughthanks
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.