Skip to main content
Misleading words about "length"
Source Link
Toby Speight
  • 6.5k
  • 1
  • 24
  • 41

Challenge

This is a simple one: Given a positive integer up to 1,000,000, return the closest prime number.

If the number itself is prime, then you should return that number; if there are two primes of equal length away fromequally close to the provided number, return the lower of the two.

Input is in the form of a single integer, and output should be in the form of an integer as well.

I don't care how you take in the input (function, STDIN, etc.) or display the output (function, STDOUT, etc.), as long as it works.

This is code golf, so standard rules apply—the program with the least bytes wins!

Test Cases

Input  =>  Output
------    -------
80     =>      79
100    =>     101
5      =>       5
9      =>       7
532    =>     523
1      =>       2

Challenge

This is a simple one: Given a positive integer up to 1,000,000, return the closest prime number.

If the number itself is prime, then you should return that number; if there are two primes of equal length away from the provided number, return the lower of the two.

Input is in the form of a single integer, and output should be in the form of an integer as well.

I don't care how you take in the input (function, STDIN, etc.) or display the output (function, STDOUT, etc.), as long as it works.

This is code golf, so standard rules apply—the program with the least bytes wins!

Test Cases

Input  =>  Output
------    -------
80     =>      79
100    =>     101
5      =>       5
9      =>       7
532    =>     523
1      =>       2

Challenge

This is a simple one: Given a positive integer up to 1,000,000, return the closest prime number.

If the number itself is prime, then you should return that number; if there are two primes equally close to the provided number, return the lower of the two.

Input is in the form of a single integer, and output should be in the form of an integer as well.

I don't care how you take in the input (function, STDIN, etc.) or display the output (function, STDOUT, etc.), as long as it works.

This is code golf, so standard rules apply—the program with the least bytes wins!

Test Cases

Input  =>  Output
------    -------
80     =>      79
100    =>     101
5      =>       5
9      =>       7
532    =>     523
1      =>       2
Remove meta-commentary about votes; improve grammar.
Source Link
Cody Gray
  • 3.5k
  • 16
  • 28

Return the Closest Prime Number

Note: This is my first question on this site. If I need to improve anything, please comment an let me know before downvoting to oblivion. Thanks!

Challenge

This is a simple one.: Given a positive integer up to 1,000,000, return the closest prime number.

If the number itself is prime, then you should return itself, andthat number; if there are two primes of equal length away from the provided number, return the lower of the two.

Input is in the form of a single integer, and output should be in the form of an integer as well.

I don't care HOWhow you take in the input (function, STDIN, etc.) or display the output (function, STDOUT, etc.), as long as it works.

This is a code golf, so standard rules apply, theapply—the program with the least bytes wins!

Test Cases

Input  =>  Output
------    -------
80     =>      79
100    =>     101
5      =>       5
9      =>       7
532    =>     523
1      =>       2

Closest Prime Number

Note: This is my first question on this site. If I need to improve anything, please comment an let me know before downvoting to oblivion. Thanks!

Challenge

This is a simple one. Given a positive integer up to 1,000,000, return the closest prime number.

If the number itself is prime, then you should return itself, and if there are two primes equal length away from the provided number, return the lower of the two.

Input is in the form of a single integer, and output should be in the form of an integer as well.

I don't care HOW you take in the input (function, STDIN, etc.) or display the output (function, STDOUT, etc.) as long as it works.

This is a code golf, so standard rules apply, the program with the least bytes wins!

Test Cases

Input  =>  Output
80     =>      79
100    =>     101
5      =>       5
9      =>       7
532    =>     523
1      =>       2

Return the Closest Prime Number

Challenge

This is a simple one: Given a positive integer up to 1,000,000, return the closest prime number.

If the number itself is prime, then you should return that number; if there are two primes of equal length away from the provided number, return the lower of the two.

Input is in the form of a single integer, and output should be in the form of an integer as well.

I don't care how you take in the input (function, STDIN, etc.) or display the output (function, STDOUT, etc.), as long as it works.

This is code golf, so standard rules apply—the program with the least bytes wins!

Test Cases

Input  =>  Output
------    -------
80     =>      79
100    =>     101
5      =>       5
9      =>       7
532    =>     523
1      =>       2
Formatted the test cases
Source Link
Gymhgy
  • 8k
  • 11
  • 35

Note: This is my first question on this site. If I need to improve anything, please comment an let me know before downvoting to oblivion. Thanks!

Challenge

This is a simple one. Given a positive integer up to 1,000,000, return the closest prime number.

If the number itself is prime, then you should return itself, and if there are two primes equal length away from the provided number, return the lower of the two.

Input is in the form of a single integer, and output should be in the form of an integer as well.

I don't care HOW you take in the input (function, STDIN, etc.) or display the output (function, STDOUT, etc.) as long as it works.

This is a code golf, so standard rules apply, the program with the least bytes wins!

ExampleTest Cases

  • Input: 80 Answer: 79
  • Input: 100 Answer: 101
  • Input: 5 Answer: 5
  • Input: 9 Answer: 7
  • Input: 532 Answer: 523
  • Input: 1 Answer: 2
Input  =>  Output
80     =>      79
100    =>     101
5      =>       5
9      =>       7
532    =>     523
1      =>       2

Note: This is my first question on this site. If I need to improve anything, please comment an let me know before downvoting to oblivion. Thanks!

Challenge

This is a simple one. Given a positive integer up to 1,000,000, return the closest prime number.

If the number itself is prime, then you should return itself, and if there are two primes equal length away from the provided number, return the lower of the two.

Input is in the form of a single integer, and output should be in the form of an integer as well.

I don't care HOW you take in the input (function, STDIN, etc.) or display the output (function, STDOUT, etc.) as long as it works.

This is a code golf, so standard rules apply, the program with the least bytes wins!

Example

  • Input: 80 Answer: 79
  • Input: 100 Answer: 101
  • Input: 5 Answer: 5
  • Input: 9 Answer: 7
  • Input: 532 Answer: 523
  • Input: 1 Answer: 2

Note: This is my first question on this site. If I need to improve anything, please comment an let me know before downvoting to oblivion. Thanks!

Challenge

This is a simple one. Given a positive integer up to 1,000,000, return the closest prime number.

If the number itself is prime, then you should return itself, and if there are two primes equal length away from the provided number, return the lower of the two.

Input is in the form of a single integer, and output should be in the form of an integer as well.

I don't care HOW you take in the input (function, STDIN, etc.) or display the output (function, STDOUT, etc.) as long as it works.

This is a code golf, so standard rules apply, the program with the least bytes wins!

Test Cases

Input  =>  Output
80     =>      79
100    =>     101
5      =>       5
9      =>       7
532    =>     523
1      =>       2
Tweeted twitter.com/StackCodeGolf/status/1110964887621169154
change list to be unordered list instead of padding with spaces.
Source Link
Loading
added 19 characters in body
Source Link
Loading
Became Hot Network Question
added 23 characters in body
Source Link
Loading
Source Link
Loading