Here we will discuss while loop statements in R and use them in creating simple functions.
For other statement types for creating algorithms, see the pages on for loop and if-else statements, and creating functions.
While loops are used to performs tasks through iterations while a set of conditions are still satisfied.
You can use logical and relational operators to specify conditions.
[1] 1
[1] 2
[1] 3
Here, an exponent of 2 is a number of the form \(2^n\), for \(n = 0, 1, 2, ...\).
The argument of interest is \(X = \{1, 2, 3. \ldots\}\).
To create the function, run the loop for numbers \(2^n <= X\):
exp2x = function(x){
n = 0
explist = c()
while(2^n <= x){
explist = c(explist, 2^n)
n = n + 1
}
return(explist)
}
You can then derive the list of exponents for \(X=1\), and \(X=1024\):
[1] 1
[1] 1 2 4 8 16 32 64 128 256 512 1024
A perfect square is the square of an integer.
The argument of interest is \(X = \{1, 2, 3. \ldots\}\).
To create the function, run the loop while the square root of numbers greater than \(X\) are not round:
You can then derive the first perfect square after \(X=45\), and \(X=100\):
[1] 49
[1] 121
The feedback form is a Google form but it does not collect any personal information.
Please click on the link below to go to the Google form.
Thank You!
Go to Feedback Form
Copyright © 2020 - 2024. All Rights Reserved by Stats Codes