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(prime, true, sizeof(prime));prime[0] = false;prime[1] = false;for (long long i = 2; i * i <= 10000000; i++){if (prime[i]){for (long long j = i * i; j <= 10000000; j += 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