Skip to content

Instantly share code, notes, and snippets.

View diakopter's full-sized avatar
💭
🤠

Matthew Wilson diakopter

💭
🤠
View GitHub Profile
sub getHammingNumbers(int $howMany, boolean $all --> List[BigInteger]) {
my $three = BigInteger.Parse("3");
my $five = BigInteger.Parse("5");
my $completedNumbers = List[BigInteger].new;
my $frontierSet = SortedSet[BigInteger].new;
$completedNumbers.Add(BigInteger.One);
$frontierSet.Add(BigInteger.op_LeftShift(BigInteger.One, 1));
$frontierSet.Add(BigInteger.One * $three);
$frontierSet.Add(BigInteger.One * $five);
loop (my $count = 1; $count < $howMany; $count++) {
my $three = BigInteger.Parse("3");
my $five = BigInteger.Parse("5");
sub updateFrontier(BigInteger $value, SortedSet[BigInteger] $frontierSet) {
$frontierSet.Add(BigInteger.op_LeftShift($value, 1));
$frontierSet.Add($value * $three);
$frontierSet.Add($value * $five)
}
sub getHammingNumbers(int $howMany, boolean $all --> List[BigInteger]) {
my $limit = 32;
sub powers_of ($radix) { 1,[\*] $radix xx * }
my @hammings =
( powers_of(2)[^ $limit ] X*
( powers_of(3)[^($limit * 2/3)] X*
powers_of(5)[^($limit * 1/2)]
)
).sort;
sub getHammingNumbers(int $howMany, boolean $all --> List[BigInteger]) {
my $three = BigInteger.Parse("3");
my $five = BigInteger.Parse("5");
my $completedNumbers = List[BigInteger].new;
my $frontierSet = SortedSet[BigInteger].new;
$completedNumbers.Add(BigInteger.One);
$frontierSet.Add(BigInteger.op_LeftShift(BigInteger.One, 1));
$frontierSet.Add(BigInteger.One * $three);
$frontierSet.Add(BigInteger.One * $five)
loop (my $count = 1; $count < $howMany; $count++) {
class knapsackItem{
has string $!name; has int $!weight;has int $!unit
method ToString(-->string) {
return self.name ~ ' ' ~ self.weight ~ ' ' ~ self.unit
}
}
sub makeItem(string $n, int $w, int $u --> knapsackItem) {
my $i = knapsackItem.new;
$i.name = $n; $i.weight = $w; $i.unit = $u;
return $i
my $count = 0;
my Int64 $i = 1;
loop (; $i <= 100000;) {
my $sqr = $i * $i;
my $sqrStr = "$sqr";
loop (my $j = 0; $j < $sqrStr.Length; $j=$j+1) {
my $left = $sqrStr.Substring(0, $j);
if ($left == "") { $left = "0"; }
my $firstNum = Int64.Parse($left);
my $secNum = Int64.Parse($sqrStr.Substring($j));
sub kaprekar( Int $n ) {
my $sq = $n ** 2;
for 0 ^..^ $sq.chars -> $i {
my $x = +$sq.substr(0, $i);
my $y = +$sq.substr($i) || return;
return True if $x + $y == $n;
}
False;
}
@diakopter
diakopter / Printf.cs
Created August 27, 2011 20:51
sprintf workarea
using Niecza;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
public partial class Builtins {
public static Variable sprintf(Variable[] args) {
using Niecza;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
public partial class Builtins {
public static Variable sprintf(Variable[] args) {
use strict;
use warnings;
package Troller;
use JSON::XS; # yum install perl-JSON-XS
use File::Slurp; # yum install perl-File-Slurp
use DirHandle;
use FileHandle;
use Time::Local;