Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
Of course it will terminate for every input.
1) The Collatz problem is about n > 0 only, but n <= 0 will be caught in the while condition and terminate right away.
2) All starting values up to 2^68 have been tested (as of 2020 btw.) to converge to the final 4,2,1 cycle, thus hitting the termination condition. Meanwhile, the int data type is at least 16 bits, usually 32 bits, but even long long int with 64 bits would be covered. -
It might be different if an implementation went for int as 128 bits, but that doesn't exist and won't exist because int with more than 32 bits would run into trouble how to define the C99+ portable data types.
int as 32 bits allows int8_t as typedef with signed char, int16_t as short int, and int32_t as int. int with 64 bits or more would leave a definition hole here. -
@iiii That's the part that is proven only for up to 2^68 (or more since 2020), not for the general case.
-
@iiii Cf. the "turkey fallacy" where the turkey reaches the maximum of feeling safe right before Thanksgiving, and then it will get butchered.
-
Wouldn't it throw an out of range exception in modern implementations of c ?
There was a time I remember if you added 1 to int.max you got int.min...
In some language. -
@AvatarOfKaine C doesn't have exceptions, and while signed integer overflow is undefined behaviour, the compiler won't be able to use this to optimise the loop condition away, given that negative values might be passed to the function.
If you get an overflow, that would make the Collatz calculation incorrect, but the question was only whether the program terminates. -
@Fast-Nop well it seems like there would be specific cases where it would.
what is the behavior of 3*int.max +1 ?
does it suddenly become a number in a different part of the positive range or does it end up as int.min ?
if thats the case it terminates.
if the number passed is negative, it terminates.
that's just my quick analysis of it.
if the language in question does not do range checking some programming languages i remember if you say have a signed word value...
so its range is something like -32512 to 32512 if you add 1 to 32512 in something like good old fashion borland pascal IF I REMEMBER, it ends up yielding -32512.
I don't know the multiplicative behavior.
I think answering this question in the case of c is knowing how the internal behavior of numbers is built into the finished native product. or even maybe in assembler.
what happens when you add 1 to 255 or do a mul 3 on a register containing 255 ? -
@Fast-Nop i mean i'm just thinking aloud.
see. it kind of matters the internal behavior.
if you wrote this in c# you'd get an exception. -
@AvatarOfKaine Interesting point. The main issue here is that int is used, not unsigned int. It might indeed be that a signed int multiplication with overflow could yield a wrong, but still positive number. That would depend on what exactly the underlying CPU is doing.
-
-
@Fast-Nop you need to consider the behavior where maxmimum values are concerned.
but if even negative or even or 0 it always terminates.
the only leftover question is what happens when it receives a value like 1/3*int.max -
this is something that kind of pisses me off actually.
if i were actively on the job i'd usually spot something like i just said.
but if not i do what i sometimes do and scan over things without thinking. -
also out of curiosity i looked it up.
that isn't actually what collatz conjecture seems to be about, because of the while loop condition.
its n!=1 -
almost like it was made for the overconfident programmer to have him skim over quick, say that's stupid, look at it again after making a half valid point, feel stupid, and look closer then compare the code to the article on something noone ever heard of and say OH AND HEY, THERE'S A CODE ERROR.
-
I'm reading this and i don't see why.
also it doesn't say anything about only positive integers, it can be handed negative too which would make sense given the 3n+1
... except no i was right the first time it wouldn't...
-1 * 3 = -3+1=-2/2=-1.....
ugghhhhhh take my pain away. -
@AvatarOfKaine The Collatz conjecture is that all starting values eventually end up in the 4,2,1 cycle, and the termination condition checks for 1.
-
@Fast-Nop yeah you mean eventually after you apply the two operations enough you either end up with a 4 or a 2 and that divides in 2 to be 1 right ?
-
@AvatarOfKaine Correct, so the termination condition basically is the Collatz conjecture.
-
@Fast-Nop you know i'm sitting here rereading our interactions.
and i'm thinking.
i have had no opportunity to really do anything that isn't pointless in so many years.
everything costs money that would provide me with novel experience and people steal everything that matters from me.
photos, reminders of emotions and things i found interesting, mercilessly just stolen.
they drive everyone crazy.
its a paradox really.
on one hand stifling creativity and reducing the things released doens't create a haystack but it also stops our culture in its tracks, while people being allowed to rewrite things to make it about some trashy numeric culture programs people neurologically thus limiting individuality.
what's the point ?
I would think travel and personal growth would be important but they started robbing me of every creative attempt i ever made, which has to be practiced to be better. -
@Fast-Nop i just don't understand because we're all going to die. we're all going to get old, why make pockets of depravity and degeneration and why trap me with it ?
I hate these creatures.
I hate people who select things just to make some stupid consistent numeric repetition. -
@Fast-Nop it is however laughable that people would choose specific combinations over and over again and think they were speaking in a secret code or shielding themselves from consequence.
-
@AvatarOfKaine no results, I just executed the above function like it was a black box XD.
But yes, the overflow happening there will surely cause bad results for many input numbers -
-
*points with his right finger at you both and a gleam in his eye and stamps his right foot* lmmfao
-
Anyway point is
This is an example
Take something vague
Make some strangely likely illogical point in some weird symbolic way
It's what they were frequently doing because they trained certain people to go crazy to a ten digit numeric code -
Which really is nonsensical
And creates nothing but confusion until personal bias solidifies reactions and actions into predicable cyclical crazy behavior -
I don't think this idiot shit was supposed to be adapted into a lifestyle
I don't think it existed before American cinema in a big way except in prostitution rings -
@Fast-Nop course I heard many different explanations and the interpretations seems individualized
-
Well. The Collatz conjecture is just one concrete example that mathematics themselves are an undecidable problem anyway.
And you’re here wasting your time on storypoints and unit tests
random