This week's code snippet, Prime Number in R, is brought to you by Subete and the Sample Programs repo.
# Program to check if the input number is prime or not
args<-commandArgs(TRUE)
if(length(args) > 0){
a1 = args[1]
# Check Numeral only, ..
numbers_only <- function(a) !grepl("\\D", a1)
if(numbers_only(a1) == TRUE){
if (a1 >= 0){
a = as.integer(a1)
if(a == 2){
cat("Prime")
# If 0, or 1 or Even Number
}else if (a == 0 || a == 1 || a %% 2 == 0 ) {
cat("Composite")
}else{
flag = 1
r = a %/% 2
for(i in 2:r) {
if (a %% i == 0) {
flag = 0
break
}
}
if(flag == 1) {
cat("Prime")
} else {
cat("Composite")
}
}
}else{# Empty Input
cat("Usage: please input a non-negative integer")
}
}else{ # Negative Input
cat("Usage: please input a non-negative integer")
}
}else{ # Empty Input
cat("Usage: please input a non-negative integer")
}
Below you'll find an up-to-date list of articles by me on The Renegade Coder. For ease of browsing, emojis let you know the article category (i.e., blog: βοΈ, code: π», meta: π, teach: π)
- π» What Is a Function in Python?
- π» What Is Operator Overloading in Python?
- π» What Is the Assignment Operator in Python?
- π As a Student, You Are a Lab Rat
- π» What Is an Expression in Python?
- π» What Is a Variable in Python?
- βοΈ A Preview of My Last Semester As a PhD Student
- π» What Is a Condition in Python?
- π» What Is a Loop in Python?
- π» What Is a Constructor in Python?
Also, here are some fun links you can use to support my work.
This document was automatically rendered on 2024-10-04 using SnakeMD.