Camel
Peter
Peter Campbell Smith

Binary aliens

Weekly challenge 305 — 20 January 2025

Week 305: 20 Jan 2025

Task 1

Task — Binary prefix

You are given a binary array. Write a script to return an array of booleans where the partial binary number up to that point is prime.

Examples


Example 1
Input: @binary = (1, 0, 1)
Output: (false, true, true)
Sub-arrays (base-10):
(1): 1 - not prime
(1, 0): 2 - prime
(1, 0, 1): 5 - prime

Example 2
Input: @binary = (1, 1, 0)
Output: (false, true, false)
Sub-arrays (base-10):
(1): 1 - not prime
(1, 1): 3 - prime
(1, 1, 0): 6 - not prime

Example 3
Input: @binary = (1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 
0, 0, 1, 0, 0, 0, 1)
Output: (false, true, true, false, false, true, false, 
false, false, false, false, false, false, false, false, 
false, false, false, false, true)

Analysis

This challenge required a little interpretation based on the supplied examples. To rephrase, we are given an array @binary of 0s and 1s. For each sub-array comprising the first 0, 1, 2 ... elements of @binary, concatenate the members of the sub-array, regard it as a number expressed in binary notation, and return 'true' or 'false' if the number is prime or not.

So let's just do it that way. To get the number resulting from elements 0 .. $j we can simply double (ie shift left) the preceding number and add $binary[$j]. All that remains is to establish whether the reult is prime, and I've used is_prime() from Math::Prime::Util to do that, but if you want a module-free result see my solution to week 146 task 1.

Try it 

Try running the script with any input:



example: 1, 0, 1, 0, 1

Script


#!/usr/bin/perl

# Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge

use v5.26;    # The Weekly Challenge - 2025-01-20
use utf8;     # Week 305 - task 1 - Binary prefix
use warnings; # Peter Campbell Smith
binmode STDOUT, ':utf8';
use Math::Prime::Util 'is_prime';

binary_prefix(1, 1, 0);
binary_prefix(1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1);
binary_prefix(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1);
binary_prefix(0, 1);

sub binary_prefix {
    
    my (@binary, $sum, $j, $result);
    
    @binary = @_;
    $sum = 0;
    
    # loop over entries in @binary
    for $j (0 .. $#binary) {
        
        # shift $sum left one bit and add next bit
        $sum = 2 * $sum + $binary[$j];
        $result .= is_prime($sum) ? 'true, ' : 'false, '
    }
    
    say qq[\nInput:  \@binary = (] . join(', ', @binary) . ')';
    say qq[Output: (] . substr($result, 0, -2) . ')';
}

Output


Input:  @binary = (1, 1, 0)
Output: (false, true, false)

Input:  @binary = (1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1,
0, 0, 1, 0, 0, 0, 1)
Output: (false, true, true, false, false, true, false,
false, false, false, false, false, false, false, false,
false, false, false, false, true)

Input:  @binary = (1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
Output: (false, true, true, false, true, false, true,
false, false, false, false, false)

Input:  @binary = (0, 1)
Output: (false, false)

 

Any content of this website which has been created by Peter Campbell Smith is in the public domain