Count of permutations of first N positive integers such that sum of any two consecutive numbers is prime
Last Updated :
22 Feb, 2023
Find the number of permutations of first N positive integers such that the sum of any two consecutive numbers is prime where all the cyclic permutations are considered the same.
Note: The sum of the first and last element must be prime as well.
Example:
Input: N = 6
Output: 2
Explanation: The two valid permutations are {1, 4, 3, 2, 5, 6} and {1, 6, 5, 2, 3, 4}. The permutation like {3, 2, 5, 6, 1, 4} is considered a cyclic permutation of the 1st one and hence not included.
Input: N = 3
Output: 0
Explanation: No valid permutations exist.
Approach: The given problem can be solved by using recursion and backtracking. It can be observed that to find the distinct number of cycles, without loss of generality, the cycle should start with 1. An isPrime[] array can be created using the Sieve of Eratosthenes which stores whether a number is prime or not. Therefore, create a recursive function and add elements in the permutation such that its sum with the last element is prime. Increment the permutation count if the sum of the first and last element is also prime.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn = 100;
ll ans = 0;
bool isPrime[maxn];
bool marked[maxn];
void SieveOfEratosthenes( int n)
{
memset (isPrime, true , sizeof (isPrime));
for ( int p = 2; p * p <= n; p++) {
if (isPrime[p] == true ) {
for ( int i = p * p; i <= n; i += p)
isPrime[i] = false ;
}
}
}
void countCycles( int m, int n, int prev, int par)
{
if (!m) {
if (isPrime[prev + 1]) {
ans++;
}
return ;
}
for ( int i = 1 + par; i <= n; i++) {
if (!marked[i] && isPrime[i + prev]) {
marked[i] = true ;
countCycles(m - 1, n, i, 1 - par);
marked[i] = false ;
}
}
}
int countPermutations( int N)
{
SieveOfEratosthenes(2 * N);
memset (marked, false , sizeof (marked));
marked[1] = true ;
countCycles(N - 1, N, 1, 1);
return ans;
}
int main()
{
int N = 6;
cout << countPermutations(N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int maxn = 100 ;
static int ans = 0 ;
static boolean []isPrime = new boolean [maxn];
static boolean []marked = new boolean [maxn];
static void SieveOfEratosthenes( int n)
{
for ( int i = 0 ; i <isPrime.length; i += 1 ) {
isPrime[i]= true ;
}
for ( int p = 2 ; p * p <= n; p++) {
if (isPrime[p] == true ) {
for ( int i = p * p; i <= n; i += p)
isPrime[i] = false ;
}
}
}
static void countCycles( int m, int n, int prev, int par)
{
if (m== 0 ) {
if (isPrime[prev + 1 ]) {
ans++;
}
return ;
}
for ( int i = 1 + par; i <= n; i++) {
if (!marked[i] && isPrime[i + prev]) {
marked[i] = true ;
countCycles(m - 1 , n, i, 1 - par);
marked[i] = false ;
}
}
}
static int countPermutations( int N)
{
SieveOfEratosthenes( 2 * N);
for ( int i = 0 ; i <marked.length; i += 1 ) {
marked[i]= false ;
}
marked[ 1 ] = true ;
countCycles(N - 1 , N, 1 , 1 );
return ans;
}
public static void main(String[] args)
{
int N = 6 ;
System.out.print(countPermutations(N));
}
}
|
Python3
import math
maxn = 100
ans = 0
isPrime = [ True for _ in range (maxn)]
marked = [ False for _ in range (maxn)]
def SieveOfEratosthenes(n):
global ans
global isPrime
global marked
for p in range ( 2 , int (math.sqrt(n)) + 1 ):
if (isPrime[p] = = True ):
for i in range (p * p, n + 1 , p):
isPrime[i] = False
def countCycles(m, n, prev, par):
global ans
global isPrime
global marked
if ( not m):
if (isPrime[prev + 1 ]):
ans + = 1
return
for i in range ( 1 + par, n + 1 ):
if ( not marked[i] and isPrime[i + prev]):
marked[i] = True
countCycles(m - 1 , n, i, 1 - par)
marked[i] = False
def countPermutations(N):
global ans
global isPrime
global marked
SieveOfEratosthenes( 2 * N)
marked[ 1 ] = True
countCycles(N - 1 , N, 1 , 1 )
return ans
if __name__ = = "__main__" :
N = 6
print (countPermutations(N))
|
C#
using System;
public class GFG
{
static int maxn = 100;
static int ans = 0;
static bool []isPrime = new bool [maxn];
static bool []marked = new bool [maxn];
static void SieveOfEratosthenes( int n)
{
for ( int i = 0; i < isPrime.Length; i += 1) {
isPrime[i] = true ;
}
for ( int p = 2; p * p <= n; p++) {
if (isPrime[p] == true ) {
for ( int i = p * p; i <= n; i += p)
isPrime[i] = false ;
}
}
}
static void countCycles( int m, int n, int prev, int par)
{
if (m==0) {
if (isPrime[prev + 1]) {
ans++;
}
return ;
}
for ( int i = 1 + par; i <= n; i++) {
if (!marked[i] && isPrime[i + prev]) {
marked[i] = true ;
countCycles(m - 1, n, i, 1 - par);
marked[i] = false ;
}
}
}
static int countPermutations( int N)
{
SieveOfEratosthenes(2 * N);
for ( int i = 0; i <marked.Length; i += 1) {
marked[i] = false ;
}
marked[1] = true ;
countCycles(N - 1, N, 1, 1);
return ans;
}
public static void Main( string [] args)
{
int N = 6;
Console.WriteLine(countPermutations(N));
}
}
|
Javascript
<script>
const maxn = 100;
let ans = 0;
let isPrime = new Array(maxn).fill( true );
let marked = new Array(maxn).fill( false );
function SieveOfEratosthenes(n) {
for (let p = 2; p * p <= n; p++) {
if (isPrime[p] == true ) {
for (let i = p * p; i <= n; i += p)
isPrime[i] = false ;
}
}
}
function countCycles(m, n, prev, par) {
if (!m) {
if (isPrime[prev + 1]) {
ans++;
}
return ;
}
for (let i = 1 + par; i <= n; i++) {
if (!marked[i] && isPrime[i + prev]) {
marked[i] = true ;
countCycles(m - 1, n, i, 1 - par);
marked[i] = false ;
}
}
}
function countPermutations(N)
{
SieveOfEratosthenes(2 * N);
marked[1] = true ;
countCycles(N - 1, N, 1, 1);
return ans;
}
let N = 6;
document.write(countPermutations(N));
</script>
|
Time Complexity: O(N!)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...