R Assignment 4
R Assignment 4
SESSION: - 2023-2025
NAME: -SHASWATI PAUL
YEAR: - 2ND
BATCH: -MCA 2C
STUDENT ID: - 231001271220
SUBJECT: - R Programming Lab
ASSIGNMENT 4
Develop R scripts to solve following problems:
1. To obtain the sum of the first n natural numbers for some given n
without using any formula.
Code:-
sum_natural_numbers <- function(n) {
sum <- 0
for (i in 1:n) {
sum <- sum + i
}
return(sum)
}
# Example usage
sum_natural_numbers(10)
Output:-
2. To obtain all the whole numbers within 1000 which are divisible by
some given p but not by some other given q.
Code:-
divisible_numbers <- function(p, q) {
result <- c()
for (i in 1:1000) {
if (i %% p == 0 && i %% q != 0) {
result <- c(result, i)
}
}
return(result)
}
# Example usage
divisible_numbers(5, 3)
Output:-
4. To obtain the sum of the following series for some given values of x and
n:
(a) x - x 3 3 + x 5 5 − x 7 7 + . . . . . . up to n terms.
Code:-
sum_series_a <- function(x, n) {
sum <- 0
for (i in 1:n) {
term <- ((-1)^(i+1) * x^i) / (2*i-1)
sum <- sum + term
}
return(sum)
}
# Example usage
sum_series_a(2, 5)
Output:-
(b) x 2 2 − x 4 4 + x 6 6 - . . . . . . .. up to n terms
Code:-
factorial <- function(n) {
if (n == 0 || n == 1) {
return(1)
} else {
return(n * factorial(n - 1))
}
}
# Example usage
sum_series_b(2, 5)
Output:-
(c) x 1 1! − x 3 3! + x 5 5! ±. . . . . . .. up to n terms
Code:-
sum_series_c <- function(x, n) {
sum <- 0
for (i in 1:n) {
term <- ((-1)^(i+1) * x^(2*i - 1)) / factorial(2*i - 1)
sum <- sum + term
}
return(sum)
}
# Example usage
sum_series_c(2, 5)
Output:-
(d) x 2 2! − x 4 4! + x 6 6! ±. . . . . . .. up to n terms
Code:-
sum_series_d <- function(x, n) {
sum <- 0
for (i in 1:n) {
term <- ((-1)^(i+1) * x^(2*i)) / factorial(2*i)
sum <- sum + term
}
return(sum)
}
# Example usage
sum_series_d(2, 5)
Output:-
# Example usage
sum_series_e(2, 5)
Output:-
5. Twin prime numbers are primes that differ by 2(e.g. 3 and 5, 101 and
103). To find out all twin primes within 10000.
Code:-
is_prime <- function(num) {
if (num <= 1) return(FALSE)
for (i in 2:sqrt(num)) {
if (num %% i == 0) return(FALSE)
}
return(TRUE)
}
# Example usage
find_twin_primes(10000)
Output:-
6. To find out the list of months with a Friday the 13th numbers within 50
which are divisible by some given p but not by some other given q.
Code:-
find_numbers_divisible_by_p_not_q <- function(p, q) {
numbers <- c()
for (i in 1:50) {
if (i %% p == 0 && i %% q != 0) {
numbers <- c(numbers, i)
}
}
return(numbers)
}
# Example usage
p <- 5
q <- 3
numbers <- find_numbers_divisible_by_p_not_q(p, q)
cat("Numbers within 50 that are divisible by", p, "but not by", q, "are:",
numbers)
Output:-
# Example usage
result <- find_successive_numbers(1000)
print(result)
Output:-
8. To print the value of a given amount of currency value in words.
Code:-
number_to_words <- function(num) {
ones <- c("", "one", "two", "three", "four", "five", "six", "seven", "eight",
"nine")
teens <- c("", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen",
"seventeen", "eighteen", "nineteen")
tens <- c("", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy",
"eighty", "ninety")
if (num == 0) {
return("zero")
}
return("")
}
# Example usage
amount <- 1234
result <- number_to_words(amount)
cat("The amount", amount, "in words is:", result)
Output:-
9. Twin prime numbers are primes that differ by 2(e.g. 3 and 5, 101
and 103). To find out all twin primes within 10000.
Code:-
is_prime <- function(num) {
if (num <= 1) return(FALSE)
for (i in 2:sqrt(num)) {
if (num %% i == 0) return(FALSE)
}
return(TRUE)
}
# Example usage
twin_primes <- find_twin_primes(10000)
print(twin_primes)
Output:-
10. To find out the list of months with a Friday the 13th for any given year.
Code:-
find_friday_13th <- function(year) {
months_with_friday_13th <- c()
for (month in 1:12) {
if (as.POSIXlt(paste(year, month, 13, sep = "-"))$wday == 5) {
months_with_friday_13th <- c(months_with_friday_13th, month)
}
}
return(months_with_friday_13th)
}
# Example usage
year <- 2023
months_with_friday_13th <- find_friday_13th(year)
cat("The months with a Friday the 13th in", year, "are:",
months_with_friday_13th)
Output:-
11. A Harshad number is a number that is divisible by the sum of its own
digits. For example, 1729 is a Harshad number because 1 + 7 + 2 + 9 = 19
and 1729 = 1991. Harshad numbers are also known as Niven numbers.
Develop an R script to determine whether a given number is a Harshad
number or not.
Code:-
# Example usage
number <- 1729
result <- is_harshad_number(number)
cat("Is", number, "a Harshad number?", result)
Output:-
# Example usage
amicable_numbers <- find_amicable_numbers(10000)
print(amicable_numbers)
Output:-
# Example usage
fermat_prime_tests <- test_fermat_primes(4)
print(fermat_prime_tests)
Output:-
# Example usage
number <- 7
result <- is_happy_number(number)
cat("Is", number, "a Happy number?", result)
Output:-
15. WAP to accept a number and then to check whether it is a perfect
square or not. If it is a perfect square then show a message for that; if not,
then find out the least number to be added to the number to make the given
number a perfect square. For example, if the given number is 1950, then
the least number to be added to the number to make it a perfect square is
determined as follows: 452 - 1950=75.
Code:-
check_perfect_square <- function(num) {
sqrt_num <- sqrt(num)
if (sqrt_num == floor(sqrt_num)) {
return(paste(num, "is a perfect square."))
} else {
next_perfect_square <- ceiling(sqrt_num)^2
least_number_to_add <- next_perfect_square - num
return(paste("The least number to be added to", num, "to make it a perfect
square is:", least_number_to_add))
}
}
# Example usage
number <- 1950
result <- check_perfect_square(number)
print(result)
Output:-
16. A number is said to be a Neon number if the sum of the digits of the
square of the number equals the number. For example, 9 is a Neon number.
Code:-
is_neon_number <- function(num) {
square_sum <- sum(as.numeric(unlist(strsplit(as.character(num^2), ""))))
return(square_sum == num)
}
# Example usage
number <- 9
result <- is_neon_number(number)
cat("Is", number, "a Neon number?", result)
Output:-
# Example usage
a <- 15
b <- 28
result <- is_coprime(a, b)
cat("Are", a, "and", b, "co-prime?", result)
Output:-
# Example usage
number <- 2486
result <- successors_of_even_digits(number)
cat("The product of the successors of even digits of", number, "is:", result)
Output:
-
19. A number is said to be a Oblong numbers if it a product of two
consecutive integers. Example: 12, 20, 42 etc.
Develop an R script to obtain all Oblong numbers within 100.
Code:-
find_oblong_numbers <- function(limit) {
oblong_numbers <- c()
for (i in 1:(limit-1)) {
oblong_number <- i * (i + 1)
if (oblong_number <= limit) {
oblong_numbers <- c(oblong_numbers, oblong_number)
}
}
return(oblong_numbers)
}
# Example usage
limit <- 100
result <- find_oblong_numbers(limit)
cat("Oblong numbers within", limit, "are:", result)
Output:-
20. A number is said to a Duck number if it contains a zero.Develop
an R script to obtain all Duck numbers within 100.
Code:-
is_duck_number <- function(num) {
digits <- unlist(strsplit(as.character(num), ""))
return("0" %in% digits)
}
# Example usage
limit <- 100
result <- find_duck_numbers(limit)
cat("Duck numbers within", limit, "are:", result)
Output:-
21. A number is said to a Spy number if the sum of its digits equals
the product of its digits. For example, 1124 is a Spy number.
Develop an R script to obtain all Spy numbers within 2000.
Code:-
is_spy_number <- function(num) {
digits <- as.numeric(unlist(strsplit(as.character(num), "")))
return(sum(digits) == prod(digits))
}
# Example usage
limit <- 2000
result <- find_spy_numbers(limit)
cat("Spy numbers within", limit, "are:", result)
Output:-