Skip to content

Instantly share code, notes, and snippets.

View BenGoldberg1's full-sized avatar

Ben Goldberg BenGoldberg1

View GitHub Profile
# This code is based on the python code at:
# http://rosettacode.org/wiki/Sieve_of_Eratosthenes#Fast_infinite_generator_using_a_wheel
sub primes { gather {
.take for 2, 3, 5, 7;
my @gaps := [
2,4,2,4,6,2,6,4,2,4,6,6,2,6,4,2,6,4,6,8,4,2,4,2,4,8,
6,4,6,2,4,6,2,6,6,4,2,4,6,2,6,4,2,4,2,10,2,10 ];
my sub wheel_prime_pairs() { gather {
@BenGoldberg1
BenGoldberg1 / quickshuffle.pl6
Last active August 8, 2016 02:01
Recursive quickshuffle
sub shuffle( @arr is copy ) {
my sub qshuf ( $first is copy, $cnt is copy ) {
loop {
return if $cnt <= 1;
my ($left, $right) = ($first, $first+$cnt);
SWAPLOOP: loop {
++$left >= $right and last SWAPLOOP while Bool.pick;
--$right <= $left and last SWAPLOOP while Bool.pick;
@arr[$left++, --$right] .= reverse;

This is an idea for an irc client. It would consist of several separate and distinct programs, each running asynchronously from each other.

The main program does the following:

  1. it connects to one or more irc servers
  2. it creates a listening network socket on the loopback interface, so only processes on the user's computer can connect to said socket.
  3. it spawns one or more "tasks", using popen, and passes the port of the socket (from step 2) to each task, either on the commandline or via stdin. Don't forget to escape specials in the executable's name. Maybe I'm the only person in the world with a space in my user name (and thus my home directory), but maybe there're others.
use v6;
constant DEBUGGING = False;
my class P does Iterator {
has @!outbuf;
has $!wheel-size;
has @!surely-composite;
has @!wheel-indices;
has @!primes;
has @!composite;
@BenGoldberg1
BenGoldberg1 / ff.pl
Created July 24, 2016 03:32
forest fire simulation for rosettacode
#!perl
# forest fire simulation for rosettacode
use strict;
use warnings;
use autodie;
use 5.010;
use Imager;
@BenGoldberg1
BenGoldberg1 / qsort.cpp
Last active May 9, 2016 02:23
non necusive quicksort
#include <climits>
#include <cassert>
#include <utility>
/*
** The following quicksort implementation avoids recursion by
** using an explicit stack. Furthermore, by always pushing onto
** the stack the larger portion of the problem, and looping on
** the smaller portion, we ensure that we don't need a very large
** stack.
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;
#include <iostream>
#include <deque>
typedef std::deque<unsigned> intlist;
typedef std::deque<intlist> listlist;
void my_insert( listlist & factors, unsigned factor ) {
if( factor >= factors.size() ) factors.resize( factor+1 );
factors[ factor ].push_back( factor );
}
int goldberg_compare( const void *ap, const void bp ) {
const char **aa = ap, **bb = bp;
const char *a = *aa, *b = *bb;
do {
int achars, bchars;
long aval, bval;
int shorter, cmp;
while( isspace(*a) ) ++a;
while( isspace(*b) ) ++b;
achars = strcspn( a, "0123456789" );
A hearse is a hearse, of course, of course
any nobody wants to lie in a hearse
of course, That is of course you are the cursed
and famous Mr Vald.
To the tune of Mr. Ed, of course.