Week 8
This week I went over some interesting videos. One video was from the playlist I mentioned in a previous blog and it was titled "Will it stop?". The video is about a problem that asks you to determine if the code will terminate(stop).
To determine if it will stop we can mentally plug in numbers. n being less than or equal to 1 will stop the code so the first number we can try is 2.2 goes in the loop and we get if ( 2%2 =0 )(true)
2/2 =1 and the code stops. So if n is 2 the code will stop.
3 goes in,
if (3%2 =0) (false)
3*3+3=12
if (12 %2=0) (true)
12/2 = 6 n
if (6%2 =0) (true)
6/2 =3 and we get a loop.
we plug in 4 we get if (4%2=0) (true)
4/2 =2 and we know 2 stops the code.
So from what we see it looks like even numbers will stop the code but if we look at the number 6 it turns out that is not true and we can see if when we can see that when we plug in 3. But when we plug in 8 it stops the code, which makes it look like that powers of 2 are what stops the code.(illustration of each number that work from 1-10 and the rules of the code below from the video)
Cool fact that a very similar equation at the university of Warsaw(below)This equations is said take any positive n and as it says has been verified with values up to 57646x10^18.
My code that goes through 1-16 for the first equation. In the video we decided that we can check if n is a power of two by doing n xor n-1 & n = n, which is why I have that.
ex. they use this in the video 16 (0b1000) 16-1=15(0b0111) 1000 xor 0111 = 1111 & 1000 = 1000 (true)
Also did first 100 for the second code the one that is at the university of Warsaw.(unfortunately some of them go off screen).
All of them end with a 1 so it is valid for the first 100 that I have tested.
video: https://www.youtube.com/watch?v=c4nJd0aC7sA&list=PLyqSpQzTE6M9p9pKxFGpskf4voY45T2DZ&index=6
I recently started coding this semester, so the only time I dealt with an infinite code is with a while loop function which you can easily eyeball and tell if it will go on forever. So, this was fascinating seeing that code will be so complex that you may need to use these techniques to see if it will end which has obvious applications.
ReplyDeleteVery interesting one indeed. Nice work!
ReplyDelete