Advertisement
Nordicus

Lucky Numbers

Dec 13th, 2013
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.28 KB | None | 0 0
  1. // Lucky Numbers.cpp : Defines the entry point for the console application.
  2. //
  3. #include "stdafx.h"
  4. #include <iostream>
  5.  
  6. void WaitLine()
  7. {
  8.     using namespace std;
  9.     cin.clear();
  10.     cin.ignore(255, '\n');
  11.     cin.get();
  12. }
  13.  
  14. int main()
  15. {
  16.     using namespace std;
  17.     int Max = 10004;
  18.     bool NumberList[10004]; //Declare our list.
  19.    
  20.     //Initialise all elements to true.
  21.     for (int i = 0; i < Max; i++)
  22.     {
  23.         NumberList[i] = true;
  24.     }
  25.  
  26.     //Falsify all evens (including 0)
  27.     for (int i = 0; i < Max; i++)
  28.     {
  29.         if (i % 2 == 0)
  30.         {
  31.             NumberList[i] = false;
  32.         }
  33.     }
  34.  
  35.     //Consistent variable declarations
  36.     int nMod = -1;
  37.  
  38.     //Main program logic.
  39.     for (int i = 3; i < Max; i++)
  40.     {
  41.         if (NumberList[i])
  42.         {
  43.             nMod = i - 1;
  44.             for (int j = 1; j < Max; j++)
  45.             {
  46.                 if (NumberList[j])
  47.                 {
  48.                     if (nMod == 0)
  49.                     {
  50.                         NumberList[j] = false;
  51.                         nMod = i - 1;
  52.                     }
  53.                     else
  54.                     {
  55.                         nMod--;
  56.                     }
  57.                 }
  58.             }
  59.         }
  60.     }
  61.  
  62.     //Output logic
  63.     int SearchVal = -1;
  64.     int Below = -1, Above = -1;
  65.     cin >> SearchVal;
  66.    
  67.     int k = 0;
  68.     do
  69.     {
  70.         if (NumberList[k])
  71.         {
  72.             if (k > SearchVal)
  73.             {
  74.                 Above = k;
  75.             }
  76.             else if (k < SearchVal)
  77.             {
  78.                 Below = k;
  79.             }
  80.         }
  81.     k++;
  82.     } while(Above < SearchVal);
  83.     cout << Below << " " << Above;
  84.     WaitLine();
  85.     return 0;
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement