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
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:
C++ Give a turing machine that times3 (x: positive integer) = while x≠1, do; if x is even then x=...
theory of computing 3. Let x be a positive integer represented in unary form. Construct a Turing machine to compute the function fx)-3x (replace the input by function value in unary form (e.g. qo 11 1) Design a grammar for L-(a b cho,n>o). 3. Let x be a positive integer represented in unary form. Construct a Turing machine to compute the function fx)-3x (replace the input by function value in unary form (e.g. qo 11 1) Design a grammar for...
1) Write a transition diagram for a Turing Machine that satisfies the following specifications. Do not make it skeletal -- show all possible transitions. (That means 3 transitions from each state (except the halt state) since Γ = {x, y, Δ} for this problem.) Pre: |Δ alpha Δ .... where alpha is a string of letters from {x,y} and ... are all Δ Post: if alpha is in the language described by regular expression (xy)* halt in the leftmost cell...
HALIX PROGRAMMING C++ 1. Write Halix machine code to read and add 5 values. Test cases: 1 2 3 4 5 ==>15 -1 -2 -3 -4 23 ==> 13 0 0 0 0 0 ==> 0 2. Write Halix machine code to read a value: Double it if the nu halve it when it's odd. Test cases: 14 ==> 28 0 ==>0 25==>12 -2==>-4 -7==>-3 3. Write a program that reads ans echoes (display what was just values that are...
Lab-3B Two-Way Branch Write a MIPS program to read a positive integer and to determine and display whether it is an even number or odd. A positive integer X is odd if its last digit is 1. Otherwise X is odd if X AND 1 is 1, or in other words X is even if X AND 1 is 0. Example Input/Output Give a positive number X= 41 X is an odd number Give a positive number X= 40 X...
(c++) Write a program that converts a positive integer into the Roman number system.(c++) Roman numbers. Write a program that converts a positive integer into the Roman number system. The Roman number system has digits I 1 V 5 X 10 L 50 C 100 D 500 M 1,000 Numbers are formed according to the following rules. (1) Only numbers up to 3,999 are represented. (2) As in the decimal system, the thousands, hundreds, tens, and ones are expressed separately....
Given the following pseudo code: int result; (result is 1 byte) int count; (count is 1 byte) for (result= 10, count= -10; count < result ; count++) { if(count > 2) result--; else result ++; } 1) write an assembly language program that will implement this pseudo code using a while construct. 2) write an assembly language program that will implement this pseudo code using a do-until construct Note: For both 1) and 2), do not forget to include the...
Write a complete C++ program that do following. 1) Read a positive integer from the user with proper prompt. Assume that this user is nice and he/she would not start the integer with digit 0. 2) Create a size 10 array as counters for digits 0 - 9. 3) Use a while loop for processing a) Single out the current last digit using modular 10 technique b) Increment the corresponding counter through the index derived in a). 4) At the...
PYTHON In mathematics, the Greatest Common Divisor (GCD) of two integers is the largest positive integer that divides the two numbers without a remainder. For example, the GCD of 8 and 12 is 4. Steps to calculate the GCD of two positive integers a,b using the Binary method is given below: Input: a, b integers If a<=0 or b<=0, then Return 0 Else, d = 0 while a and b are both even do a = a/2 b = b/2...
From previous homework you are already familiar with the math function f defined on positive integers as f(x)=(3x+1)/2 if x is odd and f(x)=x/2 if x is even. Given any integer var, iteratively applying this function f allows you to produce a list of integers starting from var and ending with 1. For example, when var is 6, this list of integers is 6,3,5,8,4,2,1, which has a length of 7 because this list contains 7 integers (call this list the Collatz list for 6). Write a C or C++...
4.14 LAB: Convert to binary Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary. For an integer x, the algorithm is: As long as x is greater than 0 Output x % 2 (remainder is either 0 or 1) x = x / 2 Note: The above algorithm outputs the 0's and 1's in reverse order. Ex: If the input is: 6 the output is:...