Skip to content

Instantly share code, notes, and snippets.

@AnkDos
Created September 25, 2018 22:13
Show Gist options
  • Save AnkDos/6104f7a4d4850bb4015b680381f7864c to your computer and use it in GitHub Desktop.
Save AnkDos/6104f7a4d4850bb4015b680381f7864c to your computer and use it in GitHub Desktop.
Program to find the nearest smaller or larger prime number from the given number.
def is_prime(num)
i=2
res = true
while i < num
if num % i == 0
res = false
break
end
i+=1
end
return res
end
def nearest_smaller_prime(num)
if is_prime(num) == true
return num
else
num = num -1
new_pr = nearest_smaller_prime(num)
end
end
def nearest_larger_prime(num)
if is_prime(num) == true
return num
else
num = num +1
new_pr = nearest_larger_prime(num)
end
end
inp = Integer(gets)
puts "#{nearest_larger_prime(74)}"
puts "#{nearest_smaller_prime(78)}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment