From 0a745d673b76aa9156d23636e69b64bac20e392f Mon Sep 17 00:00:00 2001 From: Sahil Afrid Farookhi <msafarookhi@gmail.com> Date: Sat, 27 Mar 2021 13:08:46 +0530 Subject: [PATCH 1/5] Prime number ruby program implementation --- maths/prime_number.rb | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 maths/prime_number.rb diff --git a/maths/prime_number.rb b/maths/prime_number.rb new file mode 100644 index 0000000..ad5abe7 --- /dev/null +++ b/maths/prime_number.rb @@ -0,0 +1,38 @@ +# A ruby program to check a given number is prime or not +# Mathematical explanation: A number which has only 2 factors i.e., 1 (one) and itself + +# Prime number check function +def is_prime_number(number) + primeFlag = false + if number>0 + if number == 1 + primeFlag = true + else + factors = 2; + while factors.to_i <= number.to_i/2 + if number.to_i % factors.to_i==0 + primeFlag = true + break + end + factors+=1 + end + end + else + primeFlag = true + end + + if !primeFlag + print "\nThe given number " + number.to_s + " is a Prime." + else + print "\nThe given number " + number.to_s + " is not a Prime." + end +end + +# prime input +is_prime_number(2) + +# Non-prime input +is_prime_number(20) + +# Negative input +is_prime_number(-21) From f1325f32bcf35eb3baee73c15ac9c79b48e22252 Mon Sep 17 00:00:00 2001 From: Sahil Afrid Farookhi <msafarookhi@gmail.com> Date: Sun, 28 Mar 2021 15:25:00 +0530 Subject: [PATCH 2/5] prime number: review comments implemented --- maths/prime_number.rb | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/maths/prime_number.rb b/maths/prime_number.rb index ad5abe7..e7fc4c7 100644 --- a/maths/prime_number.rb +++ b/maths/prime_number.rb @@ -3,28 +3,28 @@ # Prime number check function def is_prime_number(number) - primeFlag = false + prime_flag = false if number>0 if number == 1 - primeFlag = true + prime_flag = true else - factors = 2; + factors = 2 while factors.to_i <= number.to_i/2 if number.to_i % factors.to_i==0 - primeFlag = true - break + prime_flag = true + break end factors+=1 end end else - primeFlag = true + prime_flag = true end - if !primeFlag - print "\nThe given number " + number.to_s + " is a Prime." + if !prime_flag + puts "The given number #{number} is a Prime." else - print "\nThe given number " + number.to_s + " is not a Prime." + puts "The given number #{number} is not a Prime." end end From 00d1ed09739dc1f5d365421963742b2e5531deaa Mon Sep 17 00:00:00 2001 From: Sahil Afrid Farookhi <msafarookhi@gmail.com> Date: Sun, 28 Mar 2021 19:31:21 +0530 Subject: [PATCH 3/5] prime number: review comments-2 implementation --- maths/prime_number.rb | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/maths/prime_number.rb b/maths/prime_number.rb index e7fc4c7..7399d94 100644 --- a/maths/prime_number.rb +++ b/maths/prime_number.rb @@ -8,14 +8,7 @@ def is_prime_number(number) if number == 1 prime_flag = true else - factors = 2 - while factors.to_i <= number.to_i/2 - if number.to_i % factors.to_i==0 - prime_flag = true - break - end - factors+=1 - end + prime_flag = (2..number/2).any? { |i| number % i == 0 } end else prime_flag = true From a864792884bb4020c7e16675e7a848914eb4aaa4 Mon Sep 17 00:00:00 2001 From: Sahil Afrid Farookhi <msafarookhi@gmail.com> Date: Sun, 28 Mar 2021 22:25:40 +0530 Subject: [PATCH 4/5] prime number: review comments-3 implementation --- maths/prime_number.rb | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/maths/prime_number.rb b/maths/prime_number.rb index 7399d94..872459f 100644 --- a/maths/prime_number.rb +++ b/maths/prime_number.rb @@ -2,30 +2,38 @@ # Mathematical explanation: A number which has only 2 factors i.e., 1 (one) and itself # Prime number check function -def is_prime_number(number) - prime_flag = false - if number>0 +def prime_number(number) + non_prime_flag = false + if number > 0 if number == 1 - prime_flag = true + non_prime_flag = true + elsif number == 2 + non_prime_flag = false + elsif number % 2 == 0 + non_prime_flag = true else - prime_flag = (2..number/2).any? { |i| number % i == 0 } + non_prime_flag = (2..Math.sqrt(number)).any? { |i| number % i == 0 } end else - prime_flag = true + non_prime_flag = true end - if !prime_flag + if !non_prime_flag puts "The given number #{number} is a Prime." else puts "The given number #{number} is not a Prime." end end +# Non-prime input +prime_number(1) + # prime input -is_prime_number(2) +# Number 2 is an even prime number +prime_number(2) # Non-prime input -is_prime_number(20) +prime_number(20) # Negative input -is_prime_number(-21) +prime_number(-21) From 4af539f0c85c01cf02cf2736d5b229a0fa5aa883 Mon Sep 17 00:00:00 2001 From: Sahil Afrid Farookhi <msafarookhi@gmail.com> Date: Sun, 28 Mar 2021 22:45:12 +0530 Subject: [PATCH 5/5] prime number: review comments-4 implementation --- maths/prime_number.rb | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/maths/prime_number.rb b/maths/prime_number.rb index 872459f..28a22f4 100644 --- a/maths/prime_number.rb +++ b/maths/prime_number.rb @@ -3,19 +3,14 @@ # Prime number check function def prime_number(number) - non_prime_flag = false - if number > 0 - if number == 1 - non_prime_flag = true - elsif number == 2 - non_prime_flag = false - elsif number % 2 == 0 - non_prime_flag = true - else - non_prime_flag = (2..Math.sqrt(number)).any? { |i| number % i == 0 } - end - else + if number <= 1 non_prime_flag = true + elsif number == 2 + non_prime_flag = false + elsif number % 2 == 0 + non_prime_flag = true + else + non_prime_flag = (2..Math.sqrt(number)).any? { |i| number % i == 0 } end if !non_prime_flag