RogerBW's Blog

The Weekly Challenge 158: Prime Cuban Additives 31 March 2022

I’ve been doing the Weekly Challenges. The latest involved subsets of primes. (Note that this is open until 3 April 2022.)

Task 1: Additive Primes

Write a script to find out all Additive Primes <= 100.

Additive primes are prime numbers for which the sum of their decimal digits are also primes.

I already have code to generate a list of all primes up to a limit, so I'll reuse that. Digit sum is easy enough (repeated modulus, rather than splitting a string which I might do if I were writing only in Perl).

I make use of a relation: the digit sum of a number can be no higher than that number. (I think this is obvious, but I'm not up to producing a formal proof.) So the list of primes that I use to find candidates can be reused as the list of primes that I use to check whether a digit-sum is prime.

In Raku, the new code:

sub digitsum($x0) {
    my $s=0;
    my $x=$x0;
    while ($x > 0) {
        $s += $x % 10;
        $x div= 10;
    }
    return $s;
}

sub additiveprimes($mx) {
    my @o;
    my @p=genprimes($mx);
    my $ps=Set.new(@p);
    for @p -> $q {
        if ($ps{digitsum($q)}:exists) {
            @o.push($q);
        }
    }
    return @o;
}

In some of the languages it was easier to build up the testing-set of primes step by step, during the main loop.

Task 2: First Series Cuban Primes

Write a script to compute first series Cuban Primes <= 1000.

These are the primes that are also a difference between two successive cubes. (E.g. 3³ - 2³ = 27 - 8 = 19.) That can be rearranged into a generating formula: every entry will have the form 3y² + 3y + 1, or rearranged to save an operation, 3 × y × (y+1) + 1. So again I'll use my standard prime generation routine, but this time I just want them in a set, for primality testing of those candidates.

In Python:

def cuban1(mx):
  o=[]

Here's my set of primes.

  ps=set(genprimes(mx))

I don't want to reverse the generation formula to find a stopping point, so in theory I run y all the way up to the limit. Of course the result of the formula is going to be higher than I can use.

  for y in range(1,mx+1):
    q=3*y*(y+1)+1

If the result is higher, we can bail out now. (In fact this will always happen, and the for-loop will never complete.)

    if q > mx:
      break

Failing that, if the number is prime, add it to the output list.

    if q in ps:
      o.append(q)
  return o

Full code on github.

Comments on this post are now closed. If you have particular grounds for adding a late comment, comment on a more recent post quoting the URL of this one.

Search
Archive
Tags 1920s 1930s 1940s 1950s 1960s 1970s 1980s 1990s 2000s 2010s 2300ad 3d printing action advent of code aeronautics aikakirja anecdote animation anime army astronomy audio audio tech base commerce battletech bayern beer boardgaming book of the week bookmonth chain of command children chris chronicle church of no redeeming virtues cold war comedy computing contemporary cornish smuggler cosmic encounter coup covid-19 crime crystal cthulhu eternal cycling dead of winter doctor who documentary drama driving drone ecchi economics en garde espionage essen 2015 essen 2016 essen 2017 essen 2018 essen 2019 essen 2022 essen 2023 essen 2024 existential risk falklands war fandom fanfic fantasy feminism film firefly first world war flash point flight simulation food garmin drive gazebo genesys geocaching geodata gin gkp gurps gurps 101 gus harpoon historical history horror hugo 2014 hugo 2015 hugo 2016 hugo 2017 hugo 2018 hugo 2019 hugo 2020 hugo 2021 hugo 2022 hugo 2023 hugo 2024 hugo-nebula reread in brief avoid instrumented life javascript julian simpson julie enfield kickstarter kotlin learn to play leaving earth linux liquor lovecraftiana lua mecha men with beards mpd museum music mystery naval noir non-fiction one for the brow opera parody paul temple perl perl weekly challenge photography podcast politics postscript powers prediction privacy project woolsack pyracantha python quantum rail raku ranting raspberry pi reading reading boardgames social real life restaurant reviews romance rpg a day rpgs ruby rust scala science fiction scythe second world war security shipwreck simutrans smartphone south atlantic war squaddies stationery steampunk stuarts suburbia superheroes suspense television the resistance the weekly challenge thirsty meeples thriller tin soldier torg toys trailers travel type 26 type 31 type 45 vietnam war war wargaming weather wives and sweethearts writing about writing x-wing young adult
Special All book reviews, All film reviews
Produced by aikakirja v0.1