Question 1

This section contains a function to generate all combinations without replacement. It then initializes a result and asks the user for input on the number of bills they have and their values. It generates combinations without replacement for all values of k.

# Function to generate all possible combinations without replacement
combWoR <- function(n, k) {
   # Initialize result
   result <- matrix(numeric(0), nrow = 0, ncol = k)
   # Recursive combination function
   comb <- function(s, k, curComb) {
      # Base case: combination complete
      if (k == 0) {
         result <<- rbind(result, curComb)
         return()
      }
      # Recursive case: add next element
      for (i in s:(n-k+1)) {
         comb(i + 1, k - 1, c(curComb, i))
      }
   }
   # Call combination function
   comb(1, k, numeric(0))
   return(result)
}

n <- 15
x <- c(2312, 21703, 7065, 1412, 1903, 45507, 3914, 405, 183, 2360, 9678, 2811, 2370, 2312, 2222)
b <- 52581
result <- NULL

# Generate combinations without replacement for all values of k
found_combination = FALSE
for (k in 1:n) {
   result[[k]] <- combWoR(n, k)
   for (i in 1:choose(n,k)) {
      s <- 0
      for (j in 1:k)
         s <- s + x[[result[[k]][i,j]]]
      if (s == b) {
         print("The corresponding entries of the bill paid are")
         for (j in 1:k)
            print(x[[result[[k]][i,j]]])
         found_combination = TRUE
         break
      }
   }
}
## [1] "The corresponding entries of the bill paid are"
## [1] 1903
## [1] 45507
## [1] 2360
## [1] 2811

Question 2

This section defines variables using the iris dataset and generates an Andrews Plot.

# Define the variables
subdata <- iris[c(1,51,101),]
t <- seq(-180, 180, 10)
u <- (t+180)/10
x_t1 <- subdata[1,1]/sqrt(2) + subdata[1,2]*sin(t) + subdata[1,3]*cos(t) + subdata[1,4]*sin(2*t) + 1*cos(2*t)
x_t2 <- subdata[2,1]/sqrt(2) + subdata[2,2]*sin(t) + subdata[2,3]*cos(t) + subdata[2,4]*sin(2*t) + 2*cos(2*t)
x_t3 <- subdata[3,1]/sqrt(2) + subdata[3,2]*sin(t) + subdata[3,3]*cos(t) + subdata[3,4]*sin(2*t) + 3*cos(2*t)

# Plotting the values
plot(u, x_t1, xlab="t", ylab="x(t)", xlim=c(0, 37), ylim=c(-1.5, 19), type="l", main="Andrews Plot", col="red")
lines(u, x_t2, col="blue")
lines(u, x_t3, col="green")

# Add a legend to the plot
legend("topleft", legend = c("setosa", "versicolor", "virginica"), col = c("red", "blue", "green"), pch = 19, )

Question 3

This section defines the Zeller function to find the day of the week for a given date.

# Define the Zeller function
zeller <- function(d, m, y, c)
{
   m <- (10+m)%%12
   if (m == 0) {
      m <- 12
      y <- y-1 
   }
   if (m == 11)
      y <- y-1
   day <- (floor(2.6*m-0.2)+d+y+floor(y/4)+floor(c/4)-2*c)%%7
   if(day == 0)
      day = "Sunday"
   else if (day == 1)
      day = "Monday"
   else if (day == 2)
      day = "Tuesday"
   else if (day == 3)
      day = "Wednesday"
   else if (day == 4)
      day = "Thursday"
   else if (day == 5)
      day = "Friday"
   else 
      day = "Saturday"
   return (day)
}

# Input and store the date
date <- "02-08-1966"
dd <- as.integer(substr(date,1,2))
mm <- as.integer(substr(date,4,5))
ce <- as.integer(substr(date,7,8))
yr <- as.integer(substr(date,9,10))

cat("Day of week is ", zeller(dd, mm, yr, ce))
## Day of week is  Tuesday

Question 4

This section defines functions to find the number of iterations performed by the Kaprekar routine.

# Define the functions
Digits <- function(n)
{
   i <- 1
   x <- NULL
   while (n!=0)
   {
      x[i] <- n %% 10
      n <- floor(n/10)
      i <- i+1
   }
   return (x)
}
Kaprekar <- function(n)
{
   M <- m <- count <- 0
   while (n != 6174)
   {
      x <- Digits(n)
      min <- sort(x)
      m <- min[1]*10^3+min[2]*10^2+min[3]*10+min[4]
      max <- sort(x, decreasing = TRUE)
      M <- max[1]*10^3+max[2]*10^2+max[3]*10+max[4]
      count <- count + 1
      n <- M - m
   }
   return (count)
}

n <- 2023

cat("Number of interations performed are : ", Kaprekar(n))
## Number of interations performed are :  6

Question 5

This section defines a function to calculate the Poisson probability for a given value of x and lambda.

# Define the function
Poiss <- function(x, lambda)
{
   if (x==0) {
      return (exp(-1*lambda))
   } else
      return (lambda*Poiss(x-1, lambda)/x)
}

m <- 2.5
x <- 3

cat("Probability : ", Poiss(x, m))
## Probability :  0.213763

Question 6

This section defines a function to generate a sequence of pseudorandom numbers using the Park-Miller method.

# Define the function
PandM <- function(X_0)
{
   X <- NULL
   X[1] <- X_0
   for (i in 2:11)
      X[i] = 16807*X[i-1] %% (2^31 - 1)
   return (format(X[-1], scientific = FALSE))
}

n <- 12345678

print(PandM(n))
##  [1] "  207493810146" "22443732231438" " 6397360025287" "  104890369318"
##  [5] "30440069681434" "27504132062792" "22000290108041" "24666426703611"
##  [9] " 7219180778383" "25012863394512"

Question 7

This section defines bubble sort and pancake sort functions and applies them to a vector of random numbers.

# Bubble sort function
bubble_sort <- function(x) {
   n <- length(x)
   for (i in 1:(n-1)) {
      for (j in 1:(n-i)) {
         if (x[j] > x[j+1]) {
            temp <- x[j]
            x[j] <- x[j+1]
            x[j+1] <- temp
         }
      }
   }
   return (x)
}

# Pancake sort function
pancake_sort <- function(x) {
   n <- length(x)
   flip <- function(x, k) 
      return (c(rev(x[1:k]), x[(k+1):n]))
   for (i in n:2) {
      max_index <- which.max(x[1:i])
      if (max_index != i) {
         x <- flip(x, max_index)
         x <- flip(x, i)
      }
   }
   return(x)
}

# Store the values
set.seed(0208)
x <- rnorm(10)

cat("Original vector:\n")
## Original vector:
print(x)
##  [1]  0.3398108  1.7201904  0.2312531 -1.0609545 -0.3179125 -0.6973171
##  [7]  0.8585990  0.3837919 -1.2783293  2.5478380
cat("\nSorted using bubble sort:\n")
## 
## Sorted using bubble sort:
print(bubble_sort(x))
##  [1] -1.2783293 -1.0609545 -0.6973171 -0.3179125  0.2312531  0.3398108
##  [7]  0.3837919  0.8585990  1.7201904  2.5478380
cat("\nSorted using pancake sort:\n")
## 
## Sorted using pancake sort:
print(pancake_sort(x))
##  [1] -1.2783293 -1.0609545 -0.6973171 -0.3179125  0.2312531  0.3398108
##  [7]  0.3837919  0.8585990  1.7201904  2.5478380

To knit this R Markdown file with your code and its output, you can use the “Knit” button in RStudio. This will feed the .Rmd file to knitr, which executes all of the code chunks and creates a new markdown (.md) document which includes the code and save it’s output.

Note that this code (PDF) is just the subset of the actual A4.R file since it’s impossible to knit the RMarkdown file with user input, so I have to input the values. In order to be sure that my code is correct and harness the full potential of my code, you may compile the individual codes formulated per question.

I’ve written it to the best of my ability to make sure the code return a satisfactory result every time when compiled for sensible input values. I’ve also preformed the checks to make sure that the user input is correct before calling any function. All of which are not the part of this file since all the input are logically correct.