Problem Set 6 | 13 May 2020 - 20 May 2020 at 10 AM
Problem 1.
Write a while-while loop (nested while loop) that evaluates b until it’s equal to 3 and x until it’s equal to 5.
b <- 0
while (b <= 3) {
x <- 0
cat("b --------", b, end='\n')
while (x <= 5) {
cat("x", x, end='\n')
x <- x + 1
}
b <- b + 1
}
## b -------- 0
## x 0
## x 1
## x 2
## x 3
## x 4
## x 5
## b -------- 1
## x 0
## x 1
## x 2
## x 3
## x 4
## x 5
## b -------- 2
## x 0
## x 1
## x 2
## x 3
## x 4
## x 5
## b -------- 3
## x 0
## x 1
## x 2
## x 3
## x 4
## x 5
Problem 2.
Write a while-while loop (nested while loop) to check the number of palindrome numbers present between two limits.
What are palindromes? Palindromes - a word, number, phrase, or other sequence of characters which reads the same backward as forward, such as madam, racecar.
Use readline() as a way to input any upper or lower limit and a Boolean flag in your program to inform you that a palindrom was detected.
#ul <- readline("Upper limit: ")
#ll <- readline("Lower limit: ")
ul <- 25
ll <- 10
while (ul != ll) {
n <- ul
t <- ul
pal <- 0
palindrom_detected <- FALSE
while (n > 0) {
units_digit <- n %% 10
n <- floor(n / 10)
pal <- pal * 10 + units_digit
}
if (t==pal) {
cat(t, " is palindrome", end="\n")
palindrom_detected <- TRUE
}
ul = ul - 1
}
## 22 is palindrome
## 11 is palindrome
if (palindrom_detected) {
print("Detected at least one palindrome")
} else {print("Detected no palindromes")}
## [1] "Detected at least one palindrome"
# Q: Below, described your code in words. If you had to explain to someone what this code is doing and how each function, loop, or equation works, what would you say?
- First, a lower and upper bound of interest is entered by the user in the console and stored in their corresponding variables.
- Then, the lower and upper bounds are evaluated in a while loop, running until the upper bound is equal to the lower bound. (Foreshadow to later, we will increment either the upper bound or the lower bound so that each integer between the upper and lower bound is evaluated until they equal one another).
- Within the outer while loop, the upper bound is stored in two other temporary variables. This is important so that we don’t change the original upper bound value but manipulate the temporary values to create a check for whether a number is a palindrome or not.
- Within the outer while loop, a boolean flag is also coded to FALSE. When a palindrome is detected, the boolean flag will be encoded to TRUE, so that we can detected, in an alternative way, if or if there way not a boolean flag. Encoding the boolean flag to FALSE in the outer while loop is important because that flag will always be reset for each integer evaluated, and switched to TRUE later in the code if it is a palindrome.
- Within the inner loop, the upper bound stored in a temporary variable is evaluated, and its units digit is extracted by dividing the variable by 10 and retrieving its remainder via the %% operator. Then, the tens digit is restored in the same temporary variable. This will make it so when it is evaluated again the while loop, the %% operator and floor() function will drive the n to 0. This will cause you to exit the inner while loop and continue onto the outer loop with the next number to evaluate.
- Within the inner loop, the calculation, pal <- pal * 10 + units_digit, will take 2 runs through the while loop in order to switch the original number’s digits. The first time through, it will store the units digit as its only value. Then, in the second round, it will become the tens value and a new units value will be recalculated and added to the tens value.
- Within the inner loop, the reverse of the number and the original number will be compared. If both are equal, then the number is a palindrome and the boolean flag is switcehd to TRUE.
- If they are not equal, the number is not a palindrome.
- Once all numbers in the range have been evaluated, the outer while loop will terminate and the final boolean result will be printed.