Question

C++ Give a turing machine that times3 (x: positive integer) = while x≠1, do; if x is even then x=...

c++ Give a turing machine that

times3 (x: positive integer) =

while x≠1, do;

if x is even then x=x/2,

Else x=3x+1,

halt if x = power of 2

Write a program for the above pseudo code and test it for x =1, 2, ….., 200. Th3en test it for x=23478 and 39735

0 0
Add a comment Improve this question Transcribed image text
Answer #1

CODE IN C++:

#include <iostream>
#include<stdbool.h>
#include<math.h>
using namespace std;
bool checkPower(int n){
if(n==0)
return false;
return (ceil(log2(n)) == floor(log2(n)));
}
int times3(int x){
while(x!=1){
if(checkPower(x))
break;
if(x%2==0){
x = x/2 ;
}
else{
x = 3*x + 1 ;
}
}
return x;
}
int main()
{
int x=23478 ;
int output = times3(x);
cout<<"Input to the turing machine:"<<x<<endl;
cout<<"Output of the turing machine:"<<output<<endl;
x = 39735;
output = times3(x);
cout<<"Input to the turing machine:"<<x<<endl;
cout<<"Output of the turing machine:"<<output<<endl;
return 0;
}

OUTPUT:

Add a comment
Know the answer?
Add Answer to:
C++ Give a turing machine that times3 (x: positive integer) = while x≠1, do; if x is even then x=...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT