Skip to content

Instantly share code, notes, and snippets.

View BenGoldberg1's full-sized avatar

Ben Goldberg BenGoldberg1

View GitHub Profile
+"(*+*)*(*+* +*/*) **** ** * * *+*+*/* **** ** * *
*+*-*/* **** ** * * *+* ****-* **** ** * * *-(*-*/
*) ** (*+*/*) **** ** * * *-* **** ** * * *-*+*/*
**** ** * * *+* **** ** * * *-* * * ***((* - */*)
** * * *-*/*) * * **** ** * * *+*/* ****-* ***(*-*
/*)**(*+*/*) * * ***((*-*/*)** * * *-*/*) * * ****
** * * *+*/* **** ** * * *+*+*/*-* * *".split(<***>
).map: {.(|(<* * *>xx.count)).chr.print given EVAL
"(* ** *+*+*-*/*+$_)" }
use v6;
sub primes-list(Bool $use-wheel) {
my @start = $use-wheel ?? (2, 3) !! (2);
my @sieve;
my $i = @start-1;
my $p = @start[$i];
my $q = $p*$p;
my $n = $p;
my \incr = $use-wheel ?? 2 !! 1;
@start, -> *@primes {
@BenGoldberg1
BenGoldberg1 / TwoChannels.pl6
Last active September 27, 2015 22:32
One-Pass Sorting Algorithm
#!/usr/bin/env perl6
use v6;
sub sort-promise ($in, $out) {
start {
my @same;
earliest $in {
more * { push @same, $in.receive }
done * { return }
}
my ($less_c, $more_c) = Channel.new xx 2;
@BenGoldberg1
BenGoldberg1 / SupplySort.pl6
Last active October 17, 2015 18:16
Sort Using Supplies
#!/usr/bin/env perl6
use v6;
sub mysort( Supply $s ) {
my (@same, @supplies, @promises);
my $p = Promise.new;
$s.tap( -> $val {
if @same {
given $val <=> @same[0] {
when Same { push @same, $val }
when Less { @supplies[0].emit( $val ) }
@BenGoldberg1
BenGoldberg1 / LRU.pm6
Created November 3, 2015 04:12
Cache::LRU
use v6;
class Cache::LRU {
has Hash[Node] %.entries;
has Node $.lru;
class Node {
has $value is required;
has $prev is rw;
has $next is rw;
method splice() {
@BenGoldberg1
BenGoldberg1 / ThreadSort.pl6
Last active November 20, 2015 03:41
Bad multithreaded sort function.
#!/usr/bin/env perl6
use v6;
sub sort-promise ($c) {
my @same;
earliest $c {
more $c { push @same, $c.receive }
done $c { return }
}
my @chans = Channel.new xx 2;
@BenGoldberg1
BenGoldberg1 / PrimesI.pl6
Created November 22, 2015 02:17
Primes Iterator with Splices
use v6;
constant DEBUGGING = False;
my class PrimesI does Iterator {
has @!primes = (2, 3);
has @!sieve = (Any xx 3);
has $!p = 3;
has $!q = $!p * $!p;
has $!n = $!p;
method pull-one returns Int {
return @!primes.shift if @!primes and @!primes[0] < 4;
@BenGoldberg1
BenGoldberg1 / PrimesWheel.pl
Last active November 22, 2015 15:34
Yet another Primes thingy
#!perl
use strict;
use warnings;
package primes;
sub new { bless [2, 3], shift }
sub next {
my ($self) = @_;
bless $self, 'primes0' if @$self == 1;
shift @$self;
import java.util.*;
import java.lang.*;
import java.io.*;
class Primes
{
public static void my_insert(ArrayList<Integer> factors, Integer factor) {
int where = factor, count = factors.size();
while( where < count && factors.get(where) != null ) where += factor;
if( where >= count ) {
<script>
(function (factors, primes) {
var a_prime = 3, a_squared_prime = 9, maybe_prime = 3;
function insertPrimeIntoSieve( factor ) {
/* This functions deliberately makes factors a sparse array */
/* It will have nulls in the places which might be prime, */
/* and primes in the places which are surely composites. */
var where = factor;
while( factors[where] )
where += factor;