Write an R function to display and return all the prime numbers within an interval you give it (e.g. between 900 and 1000).
#########################################################################################################
# FUNCTION: is_prime | Returns boolean TRUE if the given input is a prime number
#
# INPUT: value | integer to figure out if it is prime
#
# PROCESS: If input is below 2 return FALSE as all these integers are not prime. Then divide the input for
# each value from 2 to (input - 1) and if the remainder is ever 0 return FALSE as it is not prime.
#
# OUTPUT: Returns a boolean value true/false
#########################################################################################################
is_prime <- function(value) {
# neg numbers are not prime, 0 is not prime, 1 is not prime so return false on all these
if (value < 2) {
return(FALSE)
}
# because of prev start at 2
current_divisor <- 2
max_divisor <- value - 1
while (current_divisor < max_divisor) {
# if the remainder of value with any number between 2 and max_divisor it is not prime
if (value %% current_divisor == 0){
return(FALSE)
}
# each iteration tests the divisor one above the last
current_divisor <- current_divisor + 1
}
# if the number shows no exceptions to being prime, then it is prime
return(TRUE)
}
#########################################################################################################
# FUNCTION: prime_interval | Returns all numbers from the lower_bound input to the upper_bound input (inclusive) that are prime
#
# INPUT: lower_bound | integer of floor of numbers to test for prime properties
# upper_bound | integer of ceiling of numbers to test for prime properties
#
# PROCESS: Loop through each number from lower_bound to upper_bound and use helper function is_prime() to determine if that number is prime. If the helper function returns true, the number is prime and add it to the running vector of prime values
#
# OUTPUT: Returns a vector of prime values
#########################################################################################################
prime_interval <- function(lower_bound, upper_bound) {
# initialize list of primes
prime_values <- c()
# start testing primes from lower bound
current_value <- lower_bound
# test until reaching and including upper_bound
while (current_value <= upper_bound) {
# use helper function "is_prime" to test if current value is prime
if (is_prime(current_value)){
# if prime, add to list of primes
prime_values <- c(prime_values, current_value)
}
# each iteration tests for primes one above the last
current_value <- current_value + 1
}
# return total list of primes
return(prime_values)
}
#########################################################################################################
# FUNCTION: display_primes | Displays both the list of primes as well as their boundaries. Also prints the total number of primes found between the given bounds
#
# INPUT: prime_values | vector of integer values that were found between boundaries
# lower_bound | integer of floor of numbers tested for prime properties
# upper_bound | integer of ceiling of numbers tested for prime properties
#
# PROCESS: Cat strings displaying the primes and bounds. Then also cat the length of prime_values using the length() function
#
# OUTPUT: NaN
#########################################################################################################
display_primes <- function(prime_values, lower_bound, upper_bound) {
cat("The following are primes from", lower_bound, "to", upper_bound, ":", prime_values, "\n")
cat("There are a total of", length(prime_values), "primes", "\n")
}
# Asks the user for a lower bound to check for primes
#lower_bound <- as.integer(readline("give a lower integer bound to check for primes: "))
# Asks the user for an upper bound to check for primes
#upper_bound <- as.integer(readline("give a upper integer bound to check for primes: "))
lower_bound <- 900
upper_bound <- 1000
# Finds primes
primes <- prime_interval(lower_bound, upper_bound)
# Prints primes and info about bounds that achieved that list
display_primes(primes, lower_bound, upper_bound)
## The following are primes from 900 to 1000 : 907 911 919 929 937 941 947 953 967 971 977 983 991 997
## There are a total of 14 primes
############################################################################
# FUNCTION: find_primes | Quantifies primes by "filtering" out non-primes.
# INPUT: lower bound and upper bound as integers.
# OUTPUT: Returns a vector of prime numbers as integers.
############################################################################
find_primes <- function(lower, upper) {
cat("Prime numbers between", lower, "and", upper, "are:", end="\n")
primes <- c()
for (num in seq(lower, upper, 1)) {
if (num > 1) { # all prime numbers are greater than 1
for (i in seq(2, num, 1)) {
if (num %% i == 0) {
break
}
}
if (i > lower) {
print(i)
primes <- c(primes, i)
}
}
}
return(primes)
}
p <- find_primes(900,1000)
## Prime numbers between 900 and 1000 are:
## [1] 907
## [1] 911
## [1] 919
## [1] 929
## [1] 937
## [1] 941
## [1] 947
## [1] 953
## [1] 967
## [1] 971
## [1] 977
## [1] 983
## [1] 991
## [1] 997