Kodemonk

WILLITST – Will it ever stop

Problem: 

When Bob was in library in University of Warsaw he saw on one of facades caption :”Will it ever stop?” and below some mysterious code:

while n > 1
  if n mod 2 = 0 then
    n:=n/2
  else
    n:=3*n+3

Help him finding it out !

Input

In first line one number n<=10^14.

Read More Here


Hint:  Manually check for number up to 17. What happens?


Code:

#include <iostream>
using namespace std;
typedef long long int lli;

bool isPoweOf2(lli number){
    
    if(number & (number-1))
        return false;
    else 
        return true;
}

int main() {
	lli number;
	cin>>number;
	
	if(isPoweOf2(number))
	    cout<<"TAK"<<endl;
	else
	    cout<<"NIE"<<endl;
	
	return 0;
}


Explanation:  When you try to see for small number, Then you notice that for number  which is in the form of power of 2, given code will terminate.

Want to improve your mathematical skill in computer science? Try this book The Art of Computer Programming: Volume 1: Fundamental Algorithms

Share This:

Next Post

Previous Post

© 2017 Kodemonk

Theme by Anders Norén