Kummer Numbers
Last Updated :
28 Feb, 2022
Given an integer N, the task is to print the first N terms of the Kummer Number.
Examples:
Input: N = 3
Output: 1, 5, 29
1 = -1 + 2
5 = -1 + 2 * 3
29 = -1 + 2 * 3 * 5
Input: N = 5
Output: 1, 5, 29, 209, 2309
A Naive Approach is to check all numbers from 1 to n one by one is prime or not, if yes then store the multiplication in result, similarly store the result of multiplication of primes till n and add -1.
Efficient Approach is to find all the prime up-to n using Sieve of Sundaram and then just calculate the nth kummer number by multiplying them all and adding -1.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
const int MAX = 1000000;
vector< int > primes;
void sieveSundaram()
{
bool marked[MAX / 2 + 1] = { 0 };
for ( int i = 1; i <= ( sqrt (MAX) - 1) / 2; i++)
for ( int j = (i * (i + 1)) << 1;
j <= MAX / 2;
j += 2 * i + 1)
marked[j] = true ;
primes.push_back(2);
for ( int i = 1; i <= MAX / 2; i++)
if (marked[i] == false )
primes.push_back(2 * i + 1);
}
int calculateKummer( int n)
{
int result = 1;
for ( int i = 0; i < n; i++)
result = result * primes[i];
return -1 + result;
}
int main()
{
int n = 5;
sieveSundaram();
for ( int i = 1; i <= n; i++)
cout << calculateKummer(i) << ", " ;
return 0;
}
|
Java
import java.util.*;
class GFG{
static int MAX = 1000000 ;
static Vector<Integer> primes = new Vector<Integer>();
static void sieveSundaram()
{
boolean marked[] = new boolean [MAX / 2 + 1 ];
for ( int i = 1 ;
i <= (Math.sqrt(MAX) - 1 ) / 2 ;
i++)
for ( int j = (i * (i + 1 )) << 1 ;
j <= MAX / 2 ;
j += 2 * i + 1 )
marked[j] = true ;
primes.add( 2 );
for ( int i = 1 ; i <= MAX / 2 ; i++)
if (marked[i] == false )
primes.add( 2 * i + 1 );
}
static int calculateKummer( int n)
{
int result = 1 ;
for ( int i = 0 ; i < n; i++)
result = result * primes.get(i);
return - 1 + result;
}
public static void main(String[] args)
{
int n = 5 ;
sieveSundaram();
for ( int i = 1 ; i <= n; i++)
System.out.print(calculateKummer(i) + ", " );
}
}
|
Python3
import math
MAX = 1000000
primes = []
def sieveSundaram():
marked = [ 0 ] * int ( MAX / 2 + 1 )
for i in range ( 1 , int ((math.sqrt( MAX ) - 1 ) / / 2 ) + 1 ):
for j in range ((i * (i + 1 )) << 1 ,
MAX / / 2 + 1 , 2 * i + 1 ):
marked[j] = True
primes.append( 2 )
for i in range ( 1 , MAX / / 2 + 1 ):
if (marked[i] = = False ):
primes.append( 2 * i + 1 )
def calculateKummer(n):
result = 1
for i in range (n):
result = result * primes[i]
return ( - 1 + result)
N = 5
sieveSundaram()
for i in range ( 1 , N + 1 ):
print (calculateKummer(i), end = ", " )
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int MAX = 1000000;
static List< int > primes = new List< int >();
static void sieveSundaram()
{
bool []marked = new bool [MAX / 2 + 1];
for ( int i = 1;
i <= (Math.Sqrt(MAX) - 1) / 2;
i++)
for ( int j = (i * (i + 1)) << 1;
j <= MAX / 2;
j += 2 * i + 1)
marked[j] = true ;
primes.Add(2);
for ( int i = 1; i <= MAX / 2; i++)
if (marked[i] == false )
primes.Add(2 * i + 1);
}
static int calculateKummer( int n)
{
int result = 1;
for ( int i = 0; i < n; i++)
result = result * primes[i];
return -1 + result;
}
public static void Main(String[] args)
{
int n = 5;
sieveSundaram();
for ( int i = 1; i <= n; i++)
Console.Write(calculateKummer(i) + ", " );
}
}
|
Javascript
<script>
let MAX = 1000000;
let primes = [];
function sieveSundaram()
{
let marked = Array.from(
{length: MAX / 2 + 1}, (_, i) => 0);
for (let i = 1;
i <= (Math.sqrt(MAX) - 1) / 2;
i++)
for (let j = (i * (i + 1)) << 1;
j <= MAX / 2;
j += 2 * i + 1)
marked[j] = true ;
primes.push(2);
for (let i = 1; i <= MAX / 2; i++)
if (marked[i] == false )
primes.push(2 * i + 1);
}
function calculateKummer(n)
{
let result = 1;
for (let i = 0; i < n; i++)
result = result * primes[i];
return -1 + result;
}
let n = 5;
sieveSundaram();
for (let i = 1; i <= n; i++)
document.write(calculateKummer(i) + ", " );
</script>
|
Output:
1, 5, 29, 209, 2309,
References: https://oeis.org/A057588
Share your thoughts in the comments
Please Login to comment...