34 views

Uploaded by Allan

You are on page 1of 3

/usr/bin/env perl

# -*- cperl -*-

BEGIN { $^W = 1 }

use strict;

use diagnostics;

use warnings;

use Carp;

use English;

use Math::Trig;

use GD;

use constant PI => 4*atan(1);

# Allan Peda

#

# Ulam prime number spiral

# http://mathworld.wolfram.com/PrimeSpiral.html

# http://en.wikipedia.org/wiki/Ulam_spiral

#

# we iterate over a grid, but we want to follow a spiral.

# Direction is cartesian based x being horizontal motion,

# and y being vertical. Up and right are postive motion.

unless(defined($ARGV[0])){

die "Please provide a png file to save to.";

}

my $pngfile = 'ulam_spiral.png';

if($ARGV[0] =~ m/\.png$/i){

$pngfile = $ARGV[0];

} else {

print "Supplied file not suffixed with .png defaulting to \"$pngfile\".\n";

}

my $num_corners = 1600;

my @turning_points = mkturns($num_corners);

# which directions do we follow

my $max = $turning_points[$#turning_points];

print "Generating map for first $max primes.\n";

my ($x_width, $y_width) = (0,0);

$x_width = $y_width = int(sqrt($max+0.5));

my %primes = %{genprimes($max)};

my $white = $im->colorAllocate(255,255,255);

my $black = $im->colorAllocate(0,0,0);

my $origin_color = $im->colorAllocate(255,0,0);

my $position=$turning_points[0];

my ($x, $y) = (0,0);

my ($delx,$dely) = (1,0); # start out moving to the right

sub gdy { return( ($y_width/2)-shift()) }

$im->setPixel(gdx($x),gdy($y),$origin_color);

for (my $i=$position;$i<=$next_turn;++$i) {

# move to next cartesian

$x += $delx;

$y += $dely;

# print "x=$x, y=$y (i = $i)\n";

if ($i==$next_turn) {

($delx,$dely) = nextDirection($delx,$dely);

}

if ( defined($primes{$i+1})) { # origin starts at n=1, not n=0

# translation to gd coordinate system

$im->setPixel(gdx($x),gdy($y),$black);

}

}

$position = $next_turn+1;

}

open(PNGFH, ">".$pngfile)

or die "Could not open image file \"$pngfile\".";

print PNGFH $im->png;

close PNGFH;

# equation was guessed at after inspection of coordinates

# from a hand drawn cartesian spiral

sub mkturns{

my $ulim = shift;

my $len = 0;

my @rv;

foreach my $i (1..$ulim) {

my $delta = ($i%2?$i+1:$i)/2;

$len = $len+$delta;

push( @rv, $len);

}

return @rv;

}

sub genprimes {

my $max = shift;

my $sieve = '';

my %primes = ( 2 => 1 );

GUESS: for (my $guess = 2; $guess <= $max; $guess++) {

next GUESS if vec($sieve,$guess,1);

$primes{$guess} = 1;

for (my $mults = $guess * $guess; $mults <= $max; $mults += $guess) {

vec($sieve,$mults,1) = 1;

}

}

return \%primes;

}

# function modded to go past 180 degrees

{

my %memo = ();

sub nextDirection {

my ($x, $y) = (shift, shift);

my $nextoffset = undef;

if( defined($memo{$x}{$y})){

$nextoffset = $memo{$x}{$y};

} else {

my $acos = acos($x);

my $asin = asin($y);

if( $asin < 0){

$asin+=(PI*2);

}

my $offset = int(2*($acos>$asin?$acos:$asin)/PI);

# hard coded count of four directions in spiral (right, up, left down)

$nextoffset = $memo{$x}{$y} = ($offset+1)%4;

}

return to_xy($nextoffset);

}

}

{

my %memo = ();

sub to_xy {

defined($memo{$_[0]}) and return @{$memo{$_[0]}};

$memo{$_[0]}= [int(cos($_[0]*PI/2)),int(sin($_[0]*PI/2))];

return @{$memo{$_[0]}};

}

}

- f(2).txtUploaded bypraviny
- New Text DocumentUploaded byMa Sciberras
- Rust 1.0Uploaded byhfujsx
- Lab-1-final.docxUploaded byWizjohn Bactong
- f(7).txtUploaded byFanny Apaza
- f(2).txtUploaded byhilda
- SAMP ATTACHEMENTUploaded byJack
- f(7).txtUploaded byRokkamSandeep
- Doubly link list in C++Uploaded byirfan_chand_mian
- Stepping Stone and ModimethodUploaded byAvi Thakur
- mimbrinfUploaded byrachmat99
- f(8).txtUploaded byBRIGIDA CASTRO
- f(3).txtUploaded byCriIs-tina RRuiszs
- f(1).txtUploaded bycleoo2007
- Atm Courier ProjectUploaded byUtkarsh Hathi
- My ProgramUploaded byjan1508
- IP Project Class XIIUploaded byManu Gupta
- Create Worker Order FunfaUploaded byJean Carlos
- Print Star Pyramid Patterns in PythonUploaded bysulochana
- Lab3Shapes.txtUploaded byxxsunshinexx
- Sit AttributeUploaded bydanomoamdoamd
- Experiments With LoopsUploaded byJohn VonNeumann
- cases.txtUploaded byDiego Fernando Garces Cruz
- CodesUploaded byryle34
- Exam803_SampleQuestion.pdfUploaded byHimanshu Setia
- Pa MetersUploaded bymagerote
- saving in c#Uploaded byOswaldo Archundia
- angelinaveni.comUploaded byJanasro Tumangger
- java script for gpaUploaded bytimios
- Computer ProgrammingUploaded byBank

- La Asertividad Expresi n de Una Sana AutoestimaUploaded byFERRANLIMA
- Anestesi Regional Pada ObesitasUploaded byTomi Julizon
- 4-3 2 ietUploaded byapi-378531759
- Paul RussellUploaded byFin Acc
- 10 Chapter 2Uploaded byCifha Rajesh Saldhanha
- Pg Centre NotificationUploaded bysvijayashankar13202
- Ebook : How to Live Longer and Feel Better - Linus Pauling ebook PDF bookUploaded byEbook PDF
- UntitledUploaded byeurolex
- DP60 Customer PresentationUploaded bySavinirS
- 6-1 roots and radical expressionsUploaded byapi-277585828
- Dr Andres Ayuela FernandezUploaded byjovmicic
- 54262817 Implementing Cisco Quality of Service QoS v2 2 Volumes 1 2Uploaded bynobita3
- A Homeopathic Drug of Anthropoides ParadiseusUploaded byDJ_U
- freedom of expression paper final draftUploaded byapi-243956658
- 8. Msat - A Screening Test for Leptospirosis -Article 8Uploaded byKrishna Kumar
- NeurocistecircosisUploaded byAgusto Dupan
- Skype RubricUploaded byvaibhav_shukla_9
- taskalfa_1800.pdfUploaded byGanesh Kumar R
- Agar Extraction Process for GracilariacliftoniiUploaded bybuarcos
- GEC-ESTRO Recommendations III for HDR BTUploaded byClaudia Morales Ulloa
- History of Social Welfare in EuropeUploaded byNicoletta Abordo Rivero
- Pardos, Indians, And the Army of New Spain Inter-Relationships and Conflicts, 1780-1810Uploaded byChristopher Cornelio
- ZhaovSNYUUploaded byjuleywayland
- Appilcation of Epi, Health Survi, Health Infor, And Role of NurseUploaded bysreekala
- CV Ridha Zarmadi UpdtdUploaded byRizza Hurqhani
- BionoidUploaded byJohnathan Patrick Murray
- test corigentaUploaded bydavidtimi
- famous hoosier projectUploaded byapi-328110119
- KapilawasthuUploaded byCharuka Kumarapeli
- escada2Uploaded byAugust Martinez