0% found this document useful (0 votes)
15 views25 pages

R Assignment 4

Uploaded by

jitmahee
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views25 pages

R Assignment 4

Uploaded by

jitmahee
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 25

TECHNO INDIA UNIVERSITY

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:-

3. To obtain all the groups of three successive numbers within 1000


that have the property that the square of the middle one is greater
by unity than the product of the other two numbers. For example,
182= 17*19+1
Code:-
find_groups <- function() {
result <- list()
for (i in 2:999) {
if (i^2 == (i - 1) * (i + 1) + 1) {
result <- c(result, list(c(i - 1, i, i + 1)))
}
}
return(result)
}
# Example usage
find_groups()
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))
}
}

sum_series_b <- function(x, n) {


sum <- 0
for (i in 1:n) {
term <- ((-1)^(i+1) * x^(3*i - 1)) / factorial(3*i - 1)
sum <- sum + term
}
return(sum)
}

# 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:-

(e) x - x 3 2 + 3∗x 5 2∗4 − 3∗5∗x 7 2∗4∗6 + - . . . . . . up to n terms


Code:-
sum_series_e <- function(x, n) {
sum <- x
factorial_sum <- 2
for (i in 2:n) {
term <- ((-1)^(i+1) * 3 * x^(2*i - 1)) / factorial_sum
sum <- sum + term
factorial_sum <- factorial_sum + (2 * i)
}
return(sum)
}

# 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)
}

find_twin_primes <- function(limit) {


result <- c()
for (i in 2:(limit - 2)) {
if (is_prime(i) && is_prime(i + 2)) {
result <- c(result, list(c(i, i + 2)))
}
}
return(result)
}

# 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:-

7. To obtain all the groups of three successive numbers within 1000


that have the property that the square of the middle one is greater
by unity than the product of the other two numbers. For example,
182 =17*19+1
Code:-
find_successive_numbers <- function(limit) {
results <- list()
for (i in 2:(limit-1)) {
if ((i^2 - 1) == ((i-1) * (i+1))) {
results <- c(results, list(c(i-1, i, i+1)))
}
}
return(results)
}

# 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")
}

if (num < 10) {


return(ones[num + 1])
} else if (num > 10 & num < 20) {
return(teens[num - 10])
} else if (num >= 20 & num < 100) {
return(paste(tens[num %/% 10 + 1], ones[num %% 10 + 1]))
} else if (num >= 100 & num < 1000) {
return(paste(ones[num %/% 100 + 1], "hundred", number_to_words(num %
% 100)))
} else if (num >= 1000 & num < 10000) {
return(paste(ones[num %/% 1000 + 1], "thousand", number_to_words(num
%% 1000)))
}

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)
}

find_twin_primes <- function(limit) {


twin_primes <- list()
for (i in 2:(limit-2)) {
if (is_prime(i) && is_prime(i+2)) {
twin_primes <- c(twin_primes, list(c(i, i+2)))
}
}
return(twin_primes)
}

# 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:-

is_harshad_number <- function(num) {


sum_of_digits <- sum(as.numeric(unlist(strsplit(as.character(num), ""))))
return(num %% sum_of_digits == 0)
}

# Example usage
number <- 1729
result <- is_harshad_number(number)
cat("Is", number, "a Harshad number?", result)
Output:-

12. Amicable numbers are pairs of numbers, also known as friendly


numbers, each of whose aliquot parts add to give the other number. (An
aliquot part is any divisor that doesn’t include the number itself).
Code:-
sum_of_divisors <- function(num) {
divisors <- c()
for (i in 1:(num-1)) {
if (num %% i == 0) {
divisors <- c(divisors, i)
}
}
return(sum(divisors))
}

find_amicable_numbers <- function(limit) {


amicable_numbers <- list()
for (a in 1:limit) {
b <- sum_of_divisors(a)
if (b > a && sum_of_divisors(b) == a) {
amicable_numbers <- c(amicable_numbers, list(c(a, b)))
}
}
return(amicable_numbers)
}

# Example usage
amicable_numbers <- find_amicable_numbers(10000)
print(amicable_numbers)
Output:-

13. A Fermat number is a number defined by the formula Fn = 2 2 n + 1


numbers, F0 = 3, F1 = 5, F2 = 17, F3 = 257, and F4 = 65,537. It is required
to test whether all the Fermat numbers are prime or not.
Code:-
fermat_numbers <- function(n) {
return(2^(2^n) + 1)
}

is_prime <- function(num) {


if (num <= 1) return(FALSE)
for (i in 2:sqrt(num)) {
if (num %% i == 0) return(FALSE)
}
return(TRUE)
}

test_fermat_primes <- function(max_n) {


results <- list()
for (n in 0:max_n) {
fn <- fermat_numbers(n)
results <- c(results, list(c(fn, is_prime(fn))))
}
return(results)
}

# Example usage
fermat_prime_tests <- test_fermat_primes(4)
print(fermat_prime_tests)
Output:-

14. A Happy number is one which ultimately generates 1 through the


iterative process of summing of the squares of the digits.For example,7 →
(72 ) → 49 → (42 + 92 ) → 97 → (92 + 72 ) → 130 → (12 + 32 ) → 10 → 1.
So, 7 is a Happy number.But, 20 is not a happy number, since, 2 2 + 02 =4 4
2 = 16 2 1 2 + 62 =37 3 2 + 72 =58 5 2 + 82 =89 8 2 + 92 =145 1 2 + 42 + 52
=40 4 2 + 02 =4(Reappears a number obtained earlier) Develop an R script
to check a given number to ascertain whether it is a happy number or not.
Code:-
# Function to calculate the sum of the squares of digits of a number
sum_of_squares_of_digits <- function(num) {
digits <- as.numeric(unlist(strsplit(as.character(num), "")))
return(sum(digits^2))
}

# Function to check if a number is a Happy number


is_happy_number <- function(num) {
seen_numbers <- c()
while (num != 1 && !(num %in% seen_numbers)) {
seen_numbers <- c(seen_numbers, num)
num <- sum_of_squares_of_digits(num)
}
return(num == 1)
}

# 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:-

17. Two numbers are said to be co-prime if their HCF is one.


Code:-
gcd <- function(a, b) {
if (b == 0) return(a)
return(gcd(b, a %% b))
}
is_coprime <- function(a, b) {
return(gcd(a, b) == 1)
}

# Example usage
a <- 15
b <- 28
result <- is_coprime(a, b)
cat("Are", a, "and", b, "co-prime?", result)
Output:-

18. WAP to display the product of the successors of even digits of a


given number.
Code:-
successors_of_even_digits <- function(num) {
digits <- as.numeric(unlist(strsplit(as.character(num), "")))
even_digits <- digits[digits %% 2 == 0]
successors <- even_digits + 1
return(prod(successors))
}

# 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)
}

find_duck_numbers <- function(limit) {


duck_numbers <- c()
for (i in 1:limit) {
if (is_duck_number(i)) {
duck_numbers <- c(duck_numbers, i)
}
}
return(duck_numbers)
}

# 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))
}

find_spy_numbers <- function(limit) {


spy_numbers <- c()
for (i in 1:limit) {
if (is_spy_number(i)) {
spy_numbers <- c(spy_numbers, i)
}
}
return(spy_numbers)
}

# Example usage
limit <- 2000
result <- find_spy_numbers(limit)
cat("Spy numbers within", limit, "are:", result)
Output:-

You might also like