aboutsummaryrefslogtreecommitdiff
path: root/challenge-241/jo-37/perl/ch-2.pl
blob: a6b45b5be0a69e1b5c5346ad63e63232e04cee1a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/perl -s

use v5.24;
use Test2::V0 '!float';
use Math::Prime::Util 'factor';
use PDL;
use PDL::NiceSlice;

our ($tests, $examples);

run_tests() if $tests || $examples;	# does not return

die <<EOS unless @ARGV;
usage: $0 [-examples] [-tests] [N...]

-examples
    run the examples from the challenge
 
-tests
    run some tests

N...
    list of positive integers

EOS


### Input and Output

say "(@{prime_order(@ARGV)})";


### Implementation

# This task is very similar to task 2 from week 238.  Following the same
# approach.

sub prime_order {
    long(map [scalar factor $_, $_], @_)->qsortvec->((1))->unpdl;
}


### Examples and tests

sub run_tests {
    SKIP: {
        skip "examples" unless $examples;

        is prime_order(11, 8, 27, 4), [11, 4, 8, 27], 'example 1';
    }

    SKIP: {
        skip "tests" unless $tests;

        is prime_order(27, 8, 121, 49, 167, 131),
            [131, 167, 49, 121, 8, 27], 'another example';
	}

    done_testing;
    exit;
}