🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
1. Introduction
Prime numbers have always held a special place in mathematics. By definition, a prime number is a natural number greater than 1 that is not a product of two smaller natural numbers. In this guide, we'll design an R program to generate a list of prime numbers up to a user-defined number, N.
2. Program Overview
Our R program will:
1. Prompt the user to input a number N.
2. Use a loop to check numbers from 2 up to N.
3. For each number, determine if it's prime.
4. Display the list of prime numbers up to N.
3. Code Program
# Prompt user to enter N
N <- as.numeric(readline(prompt = "Enter the value of N: "))
# Function to check if a number is prime
is_prime <- function(num) {
if (num <= 1) {
return(FALSE)
}
for (i in 2:sqrt(num)) {
if (num %% i == 0) {
return(FALSE)
}
}
return(TRUE)
}
# Generate and print prime numbers up to N
cat("Prime numbers up to", N, "are:\n")
for (i in 2:N) {
if (is_prime(i)) {
cat(i, " ")
}
}
Output:
Enter the value of N: 50 Prime numbers up to 50 are: 3 5 7 11 13 17 19 23 29 31 37 41 43 47
4. Step By Step Explanation
1. The program initiates by asking the user to input a number N, which represents the upper limit for prime number generation.
2. A helper function named is_prime is defined. This function assesses whether a given number num is prime. If num is 2 or greater and is not divisible by any number between 2 and the square root of num, then it's considered prime. The choice of the square root as the upper limit for the loop is a performance optimization since a larger factor of the number would have a corresponding smaller factor that was already checked.
3. The main part of the program loops through numbers from 2 to N. For each number, the is_prime function is invoked to determine if it's prime. If it is, the number gets printed.
This R program offers an efficient and intuitive approach to generate prime numbers. With a clear understanding of the logic behind prime numbers, even larger sequences can be explored!
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment