Home

Another Prime Problem

Given a number N find the smallest number K such that it satisfies all the 3 rules -

i) K >= N .
ii) K is a prime number
iii) K ≡ 1 (mod 11 )

The expression a ≡ b (mod n), pronounced “a is congruent to b modulo n,” means that a − b is a multiple of n

Example:

Input:  n = 23
Output: 67

Approach

C++

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

bool prime[10000001];
void primeInitialize()
{
    memset(primetruesizeof(prime));
    prime[0] = false;
    prime[1] = false;
    for (long long i = 2i * i <= 10000000i++)
    {
        if (prime[i])
        {
            for (long long j = i * ij <= 10000000j += i)
                prime[j] = false;
        }
    }
}

long long anotherPrime(long long n)
{
    long long ans = n;
    while (n % 11 != 1)
        n++;
    for (long long i = n;; i += 11)
    {
        if (i % 11 == 1 && prime[i])
        {
            ans = i;
            break;
        }
    }
    return ans;
}
int main()
{

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    primeInitialize();
    long long n = 32;

    cout << anotherPrime(n<< "\n";

    return 0;
}


No comments:

Post a Comment