def is_prime(n) n.prime? end
- def is_prime(n)
return false if n < 2for x in 2.. Math.sqrt(n).roundreturn false if n % x == 0endreturn true- n.prime?
- end
require 'prime' def is_prime_check(n) n.prime? end describe "Basic Tests" do it "Low values of n" do Test.assert_equals(is_prime(1), false) Test.assert_equals(is_prime(2), true) Test.assert_equals(is_prime(3), true) Test.assert_equals(is_prime(5), true) Test.assert_equals(is_prime(7), true) Test.assert_equals(is_prime(11), true) Test.assert_equals(is_prime(13), true) Test.assert_equals(is_prime(15), false) Test.assert_equals(is_prime(18), false) Test.assert_equals(is_prime(21), false) end end describe "Random Tests" do it "Values of n between 1000 and 10e12" do 100.times do n = rand(1000..1000000000000) puts "Testing for n = #{n}" Test.assert_equals(is_prime(n), is_prime_check(n)) end end end
- require 'prime'
- def is_prime_check(n)
return false if n < 2for x in 2.. Math.sqrt(n).roundreturn false if n % x == 0endreturn true- n.prime?
- end
- describe "Basic Tests" do
- it "Low values of n" do
- Test.assert_equals(is_prime(1), false)
- Test.assert_equals(is_prime(2), true)
- Test.assert_equals(is_prime(3), true)
- Test.assert_equals(is_prime(5), true)
- Test.assert_equals(is_prime(7), true)
- Test.assert_equals(is_prime(11), true)
- Test.assert_equals(is_prime(13), true)
- Test.assert_equals(is_prime(15), false)
- Test.assert_equals(is_prime(18), false)
- Test.assert_equals(is_prime(21), false)
- end
- end
- describe "Random Tests" do
it "Values of n between 1000 and 10e12" dofor h in 1..100n = rand(1000..1000000000000)result = is_prime_check(n)res = is_prime(n)it "Testing for n = " + n.to_s doTest.assert_equals(res, result)end- it "Values of n between 1000 and 10e12" do
- 100.times do
- n = rand(1000..1000000000000)
- puts "Testing for n = #{n}"
- Test.assert_equals(is_prime(n), is_prime_check(n))
- end
- end
- end