Advertisement
Guest User

Exercise Attempt - Dewey McPherson

a guest
Sep 22nd, 2014
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  1. """
  2. This is a dummy exercise to test your level on programming.
  3. Don't consider it to be a "deal breaker". This is not an exercise
  4. you can "fail" or "pass". The idea is just to see where you are
  5. standing regarding programming knowledge.
  6.  
  7. Please be honest. Try to answer it without googling for the
  8. solution online. Of course you can Google all you want about
  9. how to write an if statement, a for, or whatever you want.
  10.  
  11. Again. Be honest, it's almost anonymous (I'm the only one that will
  12. see your answers) and it's for your best interests.
  13. """
  14.  
  15. ### Assignment ###
  16. #
  17. # Your assignment is to implement the
  18. # following function: `find_next_prime`.
  19. # As the name states, given the number `n` the
  20. # function should return the next closest prime.
  21. #
  22. # Examples:
  23. # * `find_next_prime(6)` should return 7.
  24. # * `find_next_prime(10)` should return 11.
  25. # * `find_next_prime(11)` should return 13.
  26. #
  27. # You can use whatever you want (data structures,
  28. # language features, etc).
  29. #
  30. # Unit tests would be a plus. Actually, just knowing what
  31. # they are would be a plus :)
  32. #
  33. ### End Assignment ###
  34. //Realize that I have no experience with python so I'm doing this in c++. My apologies if this is not what you wanted.
  35. // Dewey McPherson
  36. bool isPrime(n)
  37. {
  38. int i;
  39. for (i=2; i<number; i++)
  40. {
  41. if (number % i == 0)
  42. {
  43. return false;
  44. }
  45. }
  46. return true;
  47. }
  48.  
  49.  
  50.  
  51. int find_next_prime(n)
  52. {
  53. int prime = 0;
  54. while( prime == 0)
  55. {
  56. if(isPrime(n))
  57. {
  58. return n;
  59. }
  60. n++;
  61. }
  62. }
  63.  
  64. int main()
  65. {
  66. int n;
  67. printf("Please enter your number: ");
  68. scanf("%i",&n);
  69. n = find_next_prime(n);
  70. printf(" \n The next prime is %i ", n);
  71.  
  72. return 0;
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement