RabbitFarm

2021-11-21

Jort Sort the First Five Long Primes

The examples used here are from The Weekly Challenge problem statement and demonstrate the working solution.

Part 1

You are given a list of numbers. Write a script to implement JortSort. It should return true/false depending if the given list of numbers are already sorted.

Solution


use strict;
use warnings;
use boolean;

sub jort_sort{
    for(my $i=0; $i < @_ - 1; $i++){
        return false if $_[$i + 1] < $_[$i];  
    }  
    return true;
}

MAIN:{
    print jort_sort(1, 2, 3, 4, 5) . "\n";
    print jort_sort(1, 3, 2, 4, 5) . "\n";
}

Sample Run


$ perl perl/ch-1.pl
1
0

Notes

Apparently Jort Sort is a joke sort started by somebody in the JavaScript community. I didn't find it all that funny, but the code to implement it only took a quick minute.

Part 2

Write a script to generate the first 5 Long Primes.

Solution


use strict;
use warnings;
use boolean;
use LWP::UserAgent;
use constant PRIME_URL => "http://primes.utm.edu/lists/small/100000.txt";

sub get_primes{
    my @primes;  
    my $ua = new LWP::UserAgent(
        ssl_opts => {verify_hostname => 0}
    );
    my $response = $ua->get(PRIME_URL);
    my @lines = split(/\n/,$response->decoded_content);
    foreach my $line (@lines){
        my @p = split(/\s+/, $line);
        unless(@p < 10){
            push @primes, @p[1..(@p - 1)]; 
        }  
    }
    return @primes; 
}

sub divide{
    my($n, $d) = @_; 
    my @remainders;
    my $q = (int($n / $d)) . ".";
    my $r = $n % $d; 
    push @remainders, $r; 
    my @a;
    for (0 .. $d){
        $q .= int($r*10 / $d);  
        $r = $r*10 % $d;
        @a = grep { $remainders[$_] == $r } (0 .. @remainders - 1);
        last if(@a); 
        push @remainders, $r; 
    }
    my $r_i = $a[0];
    my $i = index($q, ".");
    my $decimal_part = substr($q, $i+1); 
    return substr($q, 0, $i + 1) . substr($decimal_part, 0, $r_i) . "(" . substr($q, $i + $r_i + 1) . ")";  
}   

sub long_primes_five{
    my @long_primes;
    my @primes = get_primes();
    do{
        my $prime = shift @primes;    
        my $max_repetend = $prime - 1; 
        my $repeats = true if($prime != 2 && $prime != 5); 
        if($repeats){
            my $x = divide(1, $prime, [], []); 
            $x =~ m/\((\d+)\)/;
            my $repetend = $1;
            push @long_primes, [$prime, $x] if length($repetend) == $prime - 1;   
        }
    }while(@long_primes < 5);
    return @long_primes;
}

MAIN:{
    for my $p (long_primes_five()){
        print $p->[0] . "\t" . $p->[1] . "\n";
    }
}

Sample Run


$ perl perl/ch-2.pl 
7       0.(142857)
17      0.(0588235294117647)
19      0.(052631578947368421)
23      0.(0434782608695652173913)
29      0.(0344827586206896551724137931)

Notes

This second part of the challenge was much more fun! Maybe my favorite part was that it largely re-used code from challenge 106 and also Challenge 015. Here we grab a list of pre-computed primes and then check each one for the desired property. After we find five, as required, we're done.

References

Jort Sort

Long Prime

Challenge 139

posted at: 16:34 by: Adam Russell | path: /perl | permanent link to this entry

The Weekly Challenge 139 (Prolog Solutions)

The examples used here are from the weekly challenge problem statement and demonstrate the working solution.

Part 1

You are given a list of numbers. Write a script to implement JortSort. It should return true/false depending if the given list of numbers are already sorted.

Solution


:-initialization(main).

jort([]).
jort([H0, H1|[]]):-
    H1 >= H0.
jort([H0, H1|T]):-
    H1 >= H0,
    jort([H1|T]).

main:-
    (jort([1, 2, 3, 4, 5]), format("1~n", _); format("0~n", _)),
    (jort([1, 3, 2, 4, 5]), format("1~n", _); format("0~n", _)),
    (jort([1, 2, 3, 4, 5, 6]), format("1~n", _); format("0~n", _)),
    halt.

Sample Run


$ gplc prolog/ch-1.p 
$ prolog/ch-1   
1
0
1

Notes

I had never heard of a Jort Sort before this week. Once I understand what it was, a joke function which just returns true or false based on whether a given list is already sorted, I am still not sure I really get it. Or, at least, I don't really get the "joke". Apparently it started as a JavaScript thing so maybe there is something inherently funny about the JavaScript code for it.

Anyway, this is pretty easily done in Prolog especially in this case where we only are to be given a list of numbers. The code as written only goes in one direction in that it only verifies a list as requested. This could go in the other direction with a little use of between/3 and generate a sorted list too. But would that ruin the joke? Make it funnier? I found the whole exercise so unamusing I didn't bother!

Maybe the amusing part of this whole "joke" sort was, ironically, just how stupid I found the whole thing.

References

Jort Sort

Challenge 139

posted at: 16:33 by: Adam Russell | path: /prolog | permanent link to this entry