Step Up

After completing his studies Jugnu joined a software company. his office in a huge multi-story building. today he is late for his office and there is no lift for going up.

Jugnu is tall, he has to step up in a building having N number of steps. The steps are numbered as 1 to N. he is at the Xth step and he has to reach the Yth step.

He doesn't like a prime number so he is going to skip the prime steps.

You are going to find which steps having his footprint and count of the footprint.

Example:

Input:  x = 2, y = 10

Output:

4 6 8 9 10 5

Approach:

C++

#include <bits/stdc++.h>
using namespace std;

bool prime[10001];
void primeInitialize()
{

    memset(prime, true, sizeof(prime));
    prime[0] = false;
    prime[1] = false;
    for (int i = 2; i * i <= 10000; i++)
    {
        if (prime[i])
        {
            for (int j = i * i; j <= 10000; j += i)
                prime[j] = false;
        }
    }
}

int stepUP(int x, int y)
{
    int cnt = 0;
    for (int i = x; i <= y; i++)
    {
        if (prime[i] == false)
        {
            cout << i << "\n";
            cnt++;
        }
    }
    return cnt;
}
int main()
{

    int x = 2, y = 10;

    primeInitialize();

    cout << stepUP(x, y) << "\n";

    return 0;
}


No comments:

Post a Comment