1/49
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
Use a while loop to compute the number of terms required before the product
1 · 2 · 3 · 4 · 5 · . . .
reaches above 1000.
Hint: The answer is 6. Your code should only output the number 6 at the end. You do not actually have
to do any computations, just write the code that would do the computations for you and output the correct number. You do not have to write a function.
# SOLUTION
n_terms <- 0
cum_prod <- 1
i <- 1
while (cum_prod * (i + 1) < 1000) {
n_terms <- n_terms + 1
cum_prod <- cum_prod * i
i <- i + 1
}
n_terms
[1] 6
We want to figure out how many numbers we can multiply together (starting from 1) before the product becomes bigger than 1000.
1 × 2 × 3 × ... × ? < 1000
We’ll keep track of that using a while loop in R.
You're starting with 0 terms multiplied so far.
This variable will count how many numbers we use.
n_terms <- 0
This stands for cumulative product (just a fancy word for multiplying things together).
We start at 1 because multiplying by 1 doesn't change anything.
cum_prod <- 1
We start counting from 1.
This variable will hold the number we're about to multiply by.
i <- 1
This is the loop condition.
It says:
“As long as the result of multiplying the current product by the next number (i + 1
) is still less than 1000… keep going.”
This helps stop before we go over 1000.
while (cum_prod * (i + 1) < 1000)
Inside the while
loop:
We're using one more number, so we add 1 to our count.
We multiply our current product by i
.
This is like doing:
1 × 2 × 3 × ... × i
n_terms <- n_terms + 1
cum_prod <- cum_prod * i
i <- i + 1
What does %% do in R?
It returns the remainder after division (modulo operator).
Example: 5 %% 2 returns 1.
What is the basic structure of the isleapyear() function?
isleapyear <- function(year) {
out <- FALSE
# logic here…
out
}
What does year %% 4 == 0 check?
It checks if the year is divisible by 4.
When do we set out <- TRUE in the function?
When the year is divisible by 4 (first check), and not disqualified by the 100-rule unless it passes the 400-rule.
What does the second if (year %% 100 == 0) check for?
It checks if the year is divisible by 100. If true, it disqualifies it as a leap year unless…
What does year %% 400 == 0 mean?
It checks if the year is divisible by 400. If true, it re-qualifies the year as a leap year.
What’s a cleaner, vectorized version of isleapyear()?
isleapyear <- function(year) {
(year %% 4 == 0) & ((year %% 100 != 0) | (year %% 400 == 0))
}
What does the expression (year %% 4 == 0) & ((year %% 100 != 0) | (year %% 400 == 0)) mean?
Is divisible by 4 AND
(Not divisible by 100 OR divisible by 400)
What are some example outputs?
isleapyear(3) # FALSE
isleapyear(8) # TRUE
isleapyear(200) # FALSE
isleapyear(800) # TRUE
What is the goal of the areleapyears() function?
To take a numeric vector of years and return a logical vector of the same length indicating whether each year is a leap year.
What’s the simplest way to write a vectorized version using logic?
areleapyears
What does years %% 4 == 0 do in vectorized form?
It returns a logical vector indicating which years are divisible by 4.
What is vapply() used for in this context?
It applies a function (isleapyear) to each element of a vector and ensures the output type is consistent.
areleapyears
How can a for loop be used to achieve the same goal?
areleapyears
How can you write areleapyears() without calling isleapyear()?
areleapyears
What output should you get for this input?
areleapyears(years = c(3, 8, 200, 800))
[1] FALSE TRUE FALSE TRUE
What type of data structure is big_list?
A named list with nested lists inside it.
What does big_list[[3]] return?
The third element of the top-level list, which is the \"Star Wars\" list.
What does big_list[[3]][[1]] return?
The \"characters\" list inside the \"Star Wars\" list.
What does big_list[[3]][[1]][[3]] return?
The third character in the \"characters\" list — which is R2D2.
What does big_list[[3]][[1]][[3]][[1]] return?
The \"species\" of R2D2, which is \"robot\".
What is the formula to convert feet and inches to total inches?
total_inches = feet * 12 + inches
What is the conversion factor from inches to centimeters?
1 inch = 2.54 cm
What is the R function to convert feet and inches to centimeters?
imperialtometric
What does imperialtometric(1, 2) return?
35.56 cm
What does imperialtometric(inches = 2) return?
5.08 cm
What is the formula to convert centimeters to total inches?
total_inches = cm / 2.54
How do you convert total inches to feet and inches in R?
feet
What is the R function to convert centimeters to feet and inches?
metrictoimperial
What does metrictoimperial(35.56) return?
feet = 1, inches = 2
Why use %/% and %% instead of /?
%/% gives the whole number (feet); %% gives the remainder (inches).
What is the output of this code?
x <- 1:3
y <- c(TRUE, FALSE)
x[y]
[1] 1 3
✅ Logical indexing repeats y to match length of x, so it's equivalent to x[c(TRUE, FALSE, TRUE)].
What is the output of this code?
y <- c(NA, 1)
is.na(y[2])
[1] FALSE
✅ y[2] is 1, not NA, so is.na() returns FALSE.
What is the output of this code?
z <- list(x = 1, y = 2)
z$x
[1] 1
✅ $x accesses the element named "x" in the list.
What is the output of this code?
z <- list(x = 1, y = 2)
z[1]
$x
[1] 1
✅ z[1] returns a sublist containing the first element — not just the value.
What is the output of this code?
z <- list(x = 1, y = 2)
z[[1]]
[1] 1
✅ [[1]] directly extracts the value from the first element in the list.
What is the output of this code?
z <- list(x = 1, y = 2)
names(z)
[1] "x" "y"
✅ names() returns the names of the list elements.
What is the output of this code?
x <- c("A", "B", "C")
x[1:2]
[1] "A" "B"
✅ Standard indexing with a range.
What is the output of this code?
x <- matrix(1:6, nrow = 2)
x[ , 2]
[1] 3 4
✅ Extracts the 2nd column of the matrix as a vector.
What is the output of this code?
x <- matrix(1:6, nrow = 2)
x[1, ]
[1] 1 3 5
✅ Extracts the 1st row of the matrix.
What is the output of this code?
x <- matrix(1:6, nrow = 2)
x[2, 3]
[1] 6
✅ This accesses the value in the 2nd row, 3rd column.
What is the output of this code?
x <- c(TRUE, FALSE, TRUE)
which(x)
[1] 1 3
✅ which() returns the positions of TRUE values.
What is the output of this code?
x <- c(TRUE, FALSE, TRUE)
sum(x)
[1] 2
✅ In R, TRUE = 1 and FALSE = 0, so sum() adds up the number