## Create variable `my_name` that is equal to your first name and print it
my_name <- "Mina"
cat("I am", my_name, "\n")
## I am Mina
## Sometimes you may use "print" instead, this prints just the value:
print(my_name)
## [1] "Mina"
## 1.1.1
## Create a numeric variable \texttt{my\_age} that is equal to your age
my_age <- 18
## 1.1.2
## How many seconds is there in a year? Compute this number!
seconds_in_year <- 60*60*24*365
## Assign it to a suitably named variable and print the result with a
## informative message
print(paste("There are ", seconds_in_year, "seconds in a year!"))
## [1] "There are 31536000 seconds in a year!"
## 1.1.3
## What is your age in seconds? Use the variable \texttt{my\_age}
my_age_in_seconds <- my_age * seconds_in_year
## you computed
## above and seconds-in-year, and compute it. Assign the result to a
## suitably named variable and print it.
print(my_age_in_seconds)
## [1] 567648000
# 1.1.4
## How many seconds is a typical human lifetime?
typical_human_lifetime <- 75
typical_human_lifetime_seconds <- typical_human_lifetime * seconds_in_year
print(typical_human_lifetime_seconds)
## [1] 2365200000
## The rest of the questions are in the pdf. You do not need to copy the questions
## here, but please add the question numbers!
## Good luck!
##1.2 Computing
#1
m0 <- 60 # Rest mass in kg
v <- 10 # Speed in m/s
c <- 300000 # Speed of light in km/s
# Calculate Lorentz factor
gamma <- sqrt(1 - (v^2 / c^2))
# Calculate the moving mass
m <- m0 / gamma
# Print the result
cat("The moving mass of the student is", m, "kg\n")
## The moving mass of the student is 60 kg
#2
# Calculate the difference between moving mass and rest mass
mass_difference <- m - m0
# Printing results
cat("The difference between the moving mass and rest mass is approximately", round(mass_difference, digits = 10), "\n")
## The difference between the moving mass and rest mass is approximately 3.33e-08
#3
# Constants
m0 <- 1.672e-27 # Rest mass of a proton in kg
v <- 270000 # Speed in m/s
c <- 300000 # Speed of light in km/s
# Calculate Lorentz factor
gamma <- sqrt(1 - (v^2 / c^2))
# Calculate the moving mass of the proton
m <- m0 / gamma
# Print the result
cat("The moving mass of the proton is approximately", m, "kg\n")
## The moving mass of the proton is approximately 3.835831e-27 kg
#4
# Calculate the moving mass of the proton (same calculation as before)
m <- m0 / gamma
# Calculate the ratio
ratio <- m / m0
# Print the result
cat("The moving proton is approximately", ratio, "times heavier than a stationary proton.\n")
## The moving proton is approximately 2.294157 times heavier than a stationary proton.
##2 Logical operations
#1
my_age <- 18
## How many seconds in a year?
seconds_in_year <- my_age * 60*60*24*365
I_am_old <- seconds_in_year > 650000000
if (I_am_old) {
cat("Yes, you are more than 650M seconds old.\n")
} else {
cat("No, you are not more than 650M seconds old.\n")
}
## No, you are not more than 650M seconds old.
#2
age_years <- 30 # Replace with your actual age in years
age_seconds <- age_years * 365 * 24 * 60 * 60
is_old <- age_seconds > 650000000 & age_seconds < 1000000000
if (is_old) {
cat("Yes, you are more than 650M seconds old but less than 1 billion seconds old.\n")
} else {
cat("No, you are not in the specified age range.\n")
}
## Yes, you are more than 650M seconds old but less than 1 billion seconds old.
#3
age_years <- 30 # Replace with your actual age in years
age_seconds <- age_years * 365 * 24 * 60 * 60
is_in_range <- age_seconds > 500000000 & !(age_seconds > 700000000)
if (is_in_range) {
cat("Yes, you are more than 500M seconds old but not more than 700M seconds old.\n")
} else {
cat("No, you are not in the specified age range.\n")
}
## No, you are not in the specified age range.
##3 Strings
#Install stringr package
#install.packages("stringr")
library(stringr)
#Define variables for name and age
name <- "Mina"
age <- 18
#Create the template
template <- "Hello, my name is {name} and I'm {age} years old."
# Replace placeholders with actual values
intro <- str_replace_all(template, "\\{name\\}", name)
intro <- str_replace_all(intro, "\\{age\\}", as.character(age))
# Print the intro
cat(intro)
## Hello, my name is Mina and I'm 18 years old.
#5
# Convert intro to uppercase and store it in loud_intro
loud_intro <- toupper(intro)
# Print loud_intro
cat(loud_intro, "\n")
## HELLO, MY NAME IS MINA AND I'M 18 YEARS OLD.
#6
# Count the number of times "e" appears in intro
e_count <- str_count(intro, "e")
# Print the count
cat("The letter 'e' appears", e_count, "times in intro.\n")
## The letter 'e' appears 3 times in intro.
##4 Loops(25)
#1
#Print numbers 1 to 10
for (i in 1:10) {
cat(i, "\n")
}
## 1
## 2
## 3
## 4
## 5
## 6
## 7
## 8
## 9
## 10
#2
#Print squares of numbers 1 to 10
for (i in 1:10) {
cat(i^2, "\n")
}
## 1
## 4
## 9
## 16
## 25
## 36
## 49
## 64
## 81
## 100
#3
#Use for loop to print both numbers and their squares along the lines:
for (i in 1:10) {
cat(i, "^2 =", i^2, "\n")
}
## 1 ^2 = 1
## 2 ^2 = 4
## 3 ^2 = 9
## 4 ^2 = 16
## 5 ^2 = 25
## 6 ^2 = 36
## 7 ^2 = 49
## 8 ^2 = 64
## 9 ^2 = 81
## 10 ^2 = 100
#4 Compute and print the sum of numbers from 1 to 100
sum_result <- 0
for (i in 1:100) {
sum_result <- sum_result + i
}
cat("The sum of numbers from 1 to 100 is:", sum_result, "\n")
## The sum of numbers from 1 to 100 is: 5050
#5
for (n in 10:1) {
factorial_result <- 1
for (i in 1:n) {
factorial_result <- factorial_result * i
}
cat(n, "!", "=", factorial_result, "\n")
}
## 10 ! = 3628800
## 9 ! = 362880
## 8 ! = 40320
## 7 ! = 5040
## 6 ! = 720
## 5 ! = 120
## 4 ! = 24
## 3 ! = 6
## 2 ! = 2
## 1 ! = 1