Skip to content

Instantly share code, notes, and snippets.

View BenGoldberg1's full-sized avatar

Ben Goldberg BenGoldberg1

View GitHub Profile
@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;
@BenGoldberg1
BenGoldberg1 / Primes.cpp
Last active May 3, 2016 00:29
Primes Thingy In C++
#include <iostream>
#include <deque>
typedef std::deque<int> mydeque;
void my_insert( mydeque & factors, int factor ) {
int where = factor, count = factors.size();
while( where < count && factors[where] ) where += factor;
if( where >= count ) factors.resize( where + 1 );
factors[ where ] = factor;
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;
@BenGoldberg1
BenGoldberg1 / Safer.pm
Last active December 31, 2015 02:15
Improved replacement for Safe.pm
package Safer;
$VERSION = '0.000001';
use strict;
use warnings;
use 5.010;
no overloading;
use Opcode qw,opset opset_to_hex invert_opset,;
use Scalar::Util qw,weaken blessed reftype,;
use Carp qw,croak,;
@BenGoldberg1
BenGoldberg1 / Math-BigInt-SubProcess.pm
Last active January 17, 2016 17:10
Math::BigInt::SubProcess
package Math::BigInt::SubProcess;
use 5.010;
use strict;
use warnings;
use warnings qw,FATAL io uninitialized,;
use Scalar::Util qw,weaken,;
use Fatal qw,fork waitpid pipe open close,;
use version 0.77 (); our $VERSION = version->declare('v0.0.0.2016.01.16_1');
use constant DEBUGGING => !!$::{'B::'} || defined &DB::sub;
use strict;
use warnings;
# First, I started with http://perl.plover.com/Regex/Regex.pm
# Then, I beat it with an OO shaped stick until it looked like this.
# Note: Only the parser and executer are in this file; a compiler
# based vaguely on the original is in PloverHash.pl, and a compiler
# which uses an array instead of a hash is in PloverArray.pl
use strict;
use warnings;
do 'PloverOO.pl';
package NFA;
################################################################
#
# Compile parsed regexp into representation of NFA
@BenGoldberg1
BenGoldberg1 / PloverArray.pl
Last active January 27, 2016 03:29
Array based replacement for PloverHash.pl
use strict;
use warnings;
do 'PloverOO.pl';
package NFA;
# The NFA object is simply an arrayref.
# The starting state is index 0, the final state