id/email
password
forgot password | create account
about | help | code help+videos | done | prefs
CodingBat code practice

 

jebbert@volusia.k12.fl.us primenumbers > nearestPrime
prev  |  next  |  chance

Write a method that accepts a positive integer input parameter in the range of 2 to 2000. The method returns the distance to the nearest prime number less than or equal to the input parameter. So nearestPrime(8) returns 1 because the nearest prime less than or equal to 8 is 7, which is one less than 8. nearestPrime(11) returns 0 because 11 is prime so the nearest prime is 0 away.


nearestPrime(22) → 3
nearestPrime(258) → 1
nearestPrime(1025) → 4

...Save, Compile, Run (ctrl-enter)

public int nearestPrime(int num) { }

Editor font size %:
Shorter output


Forget It! -- delete my code for this problem

Progress graphs:
 Your progress graph for this problem
 Random user progress graph for this problem
 Random Epic Progress Graph

Java Help

Misc Code Practice

Difficulty: 440

Copyright Nick Parlante 2017 - privacy