Arithmetic operators in R Programming - Time & Space Complexity
We want to see how the time to do arithmetic operations changes as we work with more numbers.
How does the number of calculations grow when we increase the input size?
Analyze the time complexity of the following code snippet.
numbers <- 1:1000
result <- 0
for (num in numbers) {
result <- result + num * 2
}
This code multiplies each number by 2 and adds it to a total sum.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Multiplying and adding each number in the list.
- How many times: Once for each number in the list (1000 times in this example).
As the list gets longer, the number of calculations grows in a straight line.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 multiplications and additions |
| 100 | 100 multiplications and additions |
| 1000 | 1000 multiplications and additions |
Pattern observation: Doubling the input doubles the work needed.
Time Complexity: O(n)
This means the time to finish grows directly with the number of items you process.
[X] Wrong: "Arithmetic operations inside a loop are instant and don't add up."
[OK] Correct: Even simple math done many times adds up, so the total time grows with the number of operations.
Understanding how simple repeated calculations add up helps you explain how programs handle data efficiently.
"What if we replaced the loop with a vectorized operation? How would the time complexity change?"
Practice
Solution
Step 1: Recall basic arithmetic operators in R
In R,*is used for multiplication,+for addition,-for subtraction, and/for division.Step 2: Identify the multiplication operator
The operator*multiplies two numbers.Final Answer:
* -> Option CQuick Check:
Multiplication operator = * [OK]
- Confusing * with + or -
- Using / for multiplication
- Using ^ for multiplication
Solution
Step 1: Understand remainder operator in R
In R, the operator%%gives the remainder after division.Step 2: Identify correct syntax for remainder
10 %% 3calculates remainder of 10 divided by 3, which is 1.Final Answer:
10 %% 3 -> Option AQuick Check:
Remainder operator = %% [OK]
- Using single % instead of %%
- Confusing integer division %/% with remainder %%
- Typing operators incorrectly like /%
result <- 5 + 3 * 2
print(result)
Solution
Step 1: Apply operator precedence in R
Multiplication (*) has higher priority than addition (+), so 3 * 2 = 6 is calculated first.Step 2: Calculate the expression
Then add 5 + 6 = 11.Final Answer:
11 -> Option BQuick Check:
5 + (3 * 2) = 11 [OK]
- Adding before multiplying
- Ignoring operator precedence
- Calculating 5 + 3 first
value <- 10 %/ 3
print(value)
Solution
Step 1: Check the operator used
The operator%/is not valid in R. Integer division uses%/%.Step 2: Identify the error type
Using%/causes a syntax error because R does not recognize it.Final Answer:
Syntax error due to incorrect operator -> Option AQuick Check:
Integer division operator is %/% not %/ [OK]
- Typing %/ instead of %/%
- Confusing remainder %% with integer division
- Expecting output despite syntax error
int_div and rem?Solution
Step 1: Recall operators for integer division and remainder
Integer division uses%/%and remainder uses%%in R.Step 2: Match operators to variables
int_div <- 17 %/% 4correctly assigns integer division result (4).rem <- 17 %% 4correctly assigns remainder (1).Final Answer:
int_div <- 17 %/% 4; rem <- 17 %% 4 -> Option DQuick Check:
Integer division %/% and remainder %% used correctly [OK]
- Swapping operators for int division and remainder
- Using invalid operator %/
- Using / instead of %/% for integer division
Ready to prove it?
Pass the test and this pattern moves unknown
