How to resolve the algorithm Smallest number k such that k+2^m is composite for all m less than k step by step in the Perl programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Smallest number k such that k+2^m is composite for all m less than k step by step in the Perl programming language

Table of Contents

Problem Statement

Generate the sequence of numbers a(k), where each k is the smallest positive integer such that k + 2m is composite for every positive integer m less than k.

Suppose k == 7; test m == 1 through m == 6. If any are prime, the test fails. Is 7 + 21 (9) prime? False Is 7 + 22 (11) prime? True So 7 is not an element of this sequence. It is only necessary to test odd natural numbers k. An even number, plus any positive integer power of 2 is always composite.

Find and display, here on this page, the first 5 elements of this sequence.

OEIS:A033919 - Odd k for which k+2^m is composite for all m < k

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Smallest number k such that k+2^m is composite for all m less than k step by step in the Perl programming language

Source code in the perl programming language

use strict;
use warnings;
use bigint;
use ntheory 'is_prime';

my $cnt;
LOOP: for my $k (2..1e10) {
    next unless 1 == $k % 2;
    for my $m (1..$k-1) {
        next LOOP if is_prime $k + (1<<$m)
    }
    print "$k ";
    last if ++$cnt == 5;
}


  

You may also check:How to resolve the algorithm Kronecker product step by step in the ALGOL 68 programming language
You may also check:How to resolve the algorithm Arithmetic-geometric mean step by step in the EasyLang programming language
You may also check:How to resolve the algorithm Mad Libs step by step in the Lua programming language
You may also check:How to resolve the algorithm Department numbers step by step in the Wren programming language
You may also check:How to resolve the algorithm Wilson primes of order n step by step in the RPL programming language