Kummer Numbers | Python3 program to find Kummer number upto n terms ; list to store all prime less than and equal to 10 ^ 6 ; Function for the Sieve of Sundaram . This function stores all prime numbers less than MAX in primes ; In general Sieve of Sundaram , produces primes smaller than ( 2 * x + 2 ) for a number given number x . Since we want primes smaller than MAX , we reduce MAX to half This list is used to separate numbers of the form i + j + 2 ij from others where 1 <= i <= j ; Main logic of Sundaram . Mark all numbers which do not generate prime number by doing 2 * i + 1 ; Since 2 is a prime number ; Print other primes . Remaining primes are of the form 2 * i + 1 such that marked [ i ] is false . ; Function to calculate nth Kummer number ; Multiply first n primes ; return nth Kummer number ; Driver code
import math NEW_LINE MAX = 1000000 NEW_LINE primes = [ ] NEW_LINE def sieveSundaram ( ) : NEW_LINE INDENT marked = [ 0 ] * int ( MAX / 2 + 1 ) NEW_LINE for i in range ( 1 , int ( ( math . sqrt ( MAX ) - 1 ) // 2 ) + 1 ) : NEW_LINE INDENT for j in range ( ( i * ( i + 1 ) ) << 1 , MAX // 2 + 1 , 2 * i + 1 ) : NEW_LINE INDENT marked [ j ] = True NEW_LINE DEDENT DEDENT primes . append ( 2 ) NEW_LINE for i in range ( 1 , MAX // 2 + 1 ) : NEW_LINE INDENT if ( marked [ i ] == False ) : NEW_LINE INDENT primes . append ( 2 * i + 1 ) NEW_LINE DEDENT DEDENT DEDENT def calculateKummer ( n ) : NEW_LINE INDENT result = 1 NEW_LINE for i in range ( n ) : NEW_LINE INDENT result = result * primes [ i ] NEW_LINE DEDENT return ( - 1 + result ) NEW_LINE DEDENT N = 5 NEW_LINE sieveSundaram ( ) NEW_LINE for i in range ( 1 , N + 1 ) : NEW_LINE INDENT print ( calculateKummer ( i ) , end = " , ▁ " ) NEW_LINE DEDENT