Cody

# Problem 44384. Find the nearest prime number

Solution 1779459

Submitted on 9 Apr 2019 by Margaret Hoeller
This solution is locked. To view this solution, you need to provide a solution of the same size or smaller.

### Test Suite

Test Status Code Input and Output
1   Pass
x = 0; y_correct = 2; assert(isequal(nearestprime(x),y_correct))

x = 0 0 x = -1 1 tf = logical 0 x = -2 2 tf = logical 1

2   Pass
x = 5; y_correct = 5; assert(isequal(nearestprime(x),y_correct))

x = 5 5

3   Pass
x = 100; y_correct = 101; assert(isequal(nearestprime(x),y_correct))

x = 100 100 x = 99 101 tf = 1×2 logical array 0 1

4   Pass
x = 500; y_correct = 499; assert(isequal(nearestprime(x),y_correct))

x = 500 500 x = 499 501 tf = 1×2 logical array 1 0

5   Pass
x = 911; y_correct = 911; assert(isequal(nearestprime(x),y_correct))

x = 911 911

6   Pass
x = 2500; y_correct = 2503; assert(isequal(nearestprime(x),y_correct))

x = 2500 2500 x = 2499 2501 tf = 1×2 logical array 0 0 x = 2498 2502 tf = 1×2 logical array 0 0 x = 2497 2503 tf = 1×2 logical array 0 1

7   Pass
x = 8000; y_correct = 7993; assert(isequal(nearestprime(x),y_correct))

x = 8000 8000 x = 7999 8001 tf = 1×2 logical array 0 0 x = 7998 8002 tf = 1×2 logical array 0 0 x = 7997 8003 tf = 1×2 logical array 0 0 x = 7996 8004 tf = 1×2 logical array 0 0 x = 7995 8005 tf = 1×2 logical array 0 0 x = 7994 8006 tf = 1×2 logical array 0 0 x = 7993 8007 tf = 1×2 logical array 1 0

8   Pass
x = 100000; y_correct = 100003; assert(isequal(nearestprime(x),y_correct))

x = 100000 100000 x = 99999 100001 tf = 1×2 logical array 0 0 x = 99998 100002 tf = 1×2 logical array 0 0 x = 99997 100003 tf = 1×2 logical array 0 1

9   Pass
x = 1300000; y_correct = 1299989; assert(isequal(nearestprime(x),y_correct))

x = 1300000 1300000 x = 1299999 1300001 tf = 1×2 logical array 0 0 x = 1299998 1300002 tf = 1×2 logical array 0 0 x = 1299997 1300003 tf = 1×2 logical array 0 0 x = 1299996 1300004 tf = 1×2 logical array 0 0 x = 1299995 1300005 tf = 1×2 logical array 0 0 x = 1299994 1300006 tf = 1×2 logical array 0 0 x = 1299993 1300007 tf = 1×2 logical array 0 0 x = 1299992 1300008 tf = 1×2 logical array 0 0 x = 1299991 1300009 tf = 1×2 logical array 0 0 x = 1299990 1300010 tf = 1×2 logical array 0 0 x = 1299989 1300011 tf = 1×2 logical array 1 0

10   Pass
x = 179424710; y_correct = 179424719; assert(isequal(nearestprime(x),y_correct))

x = 179424710 179424710 x = 179424709 179424711 tf = 1×2 logical array 0 0 x = 179424708 179424712 tf = 1×2 logical array 0 0 x = 179424707 179424713 tf = 1×2 logical array 0 0 x = 179424706 179424714 tf = 1×2 logical array 0 0 x = 179424705 179424715 tf = 1×2 logical array 0 0 x = 179424704 179424716 tf = 1×2 logical array 0 0 x = 179424703 179424717 tf = 1×2 logical array 0 0 x = 179424702 179424718 tf = 1×2 logical array 0 0 x = 179424701 179424719 tf = 1×2 logical array 0 1