P41 - A list of Goldbach compositions.

AUTHOR

Philip Potter

Specification

P41 (**) A list of Goldbach compositions.
      Given a range of integers by its lower and upper limit, print a list of
       all even numbers and their Goldbach composition.

Examples

> goldbach-list 9,20
    10 = 3 + 7
    12 = 5 + 7
    14 = 3 + 11
    16 = 3 + 13
    18 = 5 + 13
    20 = 3 + 17

In most cases, if an even number is written as the sum of two prime numbers, one of them is very small. Very rarely, the primes are both bigger than say 50. Try to find out how many such cases there are in the range 2..3000.

Example (for a print limit of 50):

> goldbach-list 1,2000,50
    992 = 73 + 919
    1382 = 61 + 1321
    1856 = 67 + 1789
    1928 = 61 + 1867
use v6;



# From P31-rhebus.pl again
sub is_prime (Int $n) {
    for 2..sqrt $n -> $k {
        return Bool::False if $n %% $k;
    }
    return Bool::True;
}

# require even arguments
sub goldbach (Int $n where {$^a > 2 && $^a %% 2}) {
    for 2..$n/2 -> $k {
        if is_prime($k) && is_prime($n-$k) {
            return ($k, $n-$k);
        }
    }
    # actually, it's more likely a logic error than a refutation :)
    die "Goldbach's conjecture is false! $n cannot be separated into two primes!"
}

# Here we demonstrate an optional parameter with a default value
sub goldbach-list (Int $low, Int $high, Int $limit = 1) {
    for $low .. $high -> $n {
        next if $n % 2; # skip invalid goldbach numbers
        next if $n == 2;
        my @pair = goldbach($n);
        say "$n = ", @pair.join(' + ') if @pair[0] > $limit;
    }
}

goldbach-list 9,20;
goldbach-list 2,1000,10;

# vim: expandtab shiftwidth=4 ft=perl6

See Also

99-problems.pod

P01-scottp.raku

P01 - Find the last box of a list.

P01-topo.raku

P01 - Find the last element of a list.

P02-scottp.raku

P02 - Find the last but one box of a list.

P02-topo.raku

P02 - Find the last two elements of a list.

P03-scottp.raku

P03 - Find the K'th element of a list.

P03-topo.raku

P03 - Find the kth element of a list.

P04-scottp.raku

P04 - Find the number of elements of a list

P04-topo.raku

P04 - Find the number of elements in a list.

P05-scottp.raku

P05 - Reverse a list

P05-topo.raku

P05 - Reverse a list.

P06-ajs.raku

P06 - Find out whether a list is a palindrome.

P06-scottp.raku

P06 - Find out whether a list is a palindrome.

P06-sdondley.raku

P06 - Find out whether a list is a palindrome.

P06-topo.raku

P06 - Find out whether a list is a palindrome.

P07-eric256.raku

P07 - Flatten a nested array structure.

P07-topo.raku

P07 - Flatten a nested array structure.

P07-viklund.raku

P07 - Flatten a nested array structure.

P08-eric256.raku

P08 - Eliminate consecutive duplicates of list elements.

P08-topo.raku

P08 - Eliminate consecutive duplicates of list elements.

P08-viklund.raku

P08 - Eliminate consecutive duplicates of list elements.

P09-rje.raku

P09 - Pack consecutive duplicates of list elements into sublists.

P09-scottp.raku

P09 - Pack consecutive duplicates of list elements into sublists.

P09-topo.raku

P09 - Pack consecutive duplicate elements of a list into sublists.

P09-unobe.raku

P09 - Pack consecutive duplicates of list elements into sublists.

P10-scottp.raku

P10 - Run-length encoding of a list.

P10-topo.raku

P10 - Run-length encoding of a list.

P10-unobe.raku

P10 - Run-length encoding of a list.

P11-topo.raku

P11 - Modified run-length encoding.

P11-unobe.raku

P11 - Modified run-length encoding.

P12-rhebus.raku

P12 - Decode a run-length encoded list.

P12-topo.raku

P12 - Decode modified run-length encoding.

P12-unobe.raku

P12 - Decode a run-length encoded list.

P13-rhebus.raku

P13 - Run-length encoding of a list (direct solution).

P13-topo.raku

P13 - Direct run-length encoding.

P13-viklund.raku

P13 - Run-length encoding of a list (direct solution).

P14-scottp.raku

P14 - Duplicate the elements of a list.

P14-topo.raku

P14 - Duplicate the elements in a list.

P14-viklund.raku

P14 - Duplicate the elements of a list.

P15-rhebus.raku

P15 - Replicate the elements of a list a given number of times.

P15-topo.raku

P15 - Replicate the elements of a list a given number of times.

P15-unobe.raku

P15 - Replicate the elements of a list a given number of times.

P16-edpratomo.raku

P16 (**) Drop every N'th element from a list.

P16-topo.raku

P16 - Drop every nth element from a list.

P17-sdondley.raku

P17 - Split a list into two parts; the length of the first part is given.

P17-topo.raku

P17 - Split a list into two parts; the length of the first part is given.

P17-unobe.raku

P17 - Split a list into two parts; the length of the first part is given.

P18-topo.raku

P18 - Extract a slice from a list. Indices start at 1.

P19-topo.raku

P19 - Rotate a list n places to the left.

P20-rhebus.raku

P20 - Remove the K'th element from a list.

P20-topo.raku

P20 - Remove the kth element of a list.

P21-scottp.raku

P21 - Insert an element at a given position into an array.

P21-topo.raku

P21 - Insert an element at a given position into a list.

P22-scottp.raku

P22 - Create a list containing all integers within a given range.

P22-topo.raku

P22 - Create a list containing all integers within a given range.

P23-topo.raku

P23 - Extract a given number of randomly selected elements from a list.

P24-topo.raku

P24 - Draw N different random numbers from the set 1..M.

P25-topo.raku

P25 - Generate a random permutation of the elements of a list.

P26-topo.raku

P26 - Generate the combinations of k distinct objects chosen from the n elements of a list.

P31-rhebus.raku

P31 - Determine whether a given integer number is prime.

P32-rhebus.raku

P32 - Determine the greatest common divisor of two positive integer

P33-rhebus.raku

P33 - Determine whether two positive integer numbers are coprime.

P34-rhebus.raku

P34 - Calculate Euler's totient function phi(m).

P35-rhebus.raku

P35 - Determine the prime factors of a given positive integer.

P36-ovid.raku

P36 - Determine the prime factors of a given positive integer (2).

P36-rhebus.raku

P36 - Determine the prime factors of a given positive integer (2).

P37-rhebus.raku

P37 - Calculate Euler's totient function phi(m) (improved).

P39-rhebus.raku

P39 - A list of prime numbers.

P40-rhebus.raku

P40 - Goldbach's conjecture.

P91-edpratomo.raku

P91 - Knight's tour.

README.md

The Camelia image is copyright 2009 by Larry Wall. "Raku" is trademark of the Yet Another Society. All rights reserved.

Built with Podlite — the markup and publishing tools behind this site.