# tsc_permute: permute each word of input permute([split], []); sub permute { my @items = @{ $_[0] }; my @perms = @{ $_[1] }; unless (@items) { print "@perms\n"; } else { my(@newitems,@newperms,$i); foreach $i (0 .. $#items) { @newitems = @items; @newperms = @perms; unshift(@newperms, splice(@newitems, $i, 1)); permute([@newitems], [@newperms]); } } }
#!/usr/bin/perl -w ############################################ ## This is a simple Temperature converter ## that will convert Fahrenheit to Celsius ## and Celsius to Fahrenheit. ############################################ use strict; my $fahr = 0; my $cel = 0; my $choice = 0; my $input = 0; print "\n"; print "*********************************************\n"; print "*** This is a Temperature Converter ***\n"; print "*********************************************\n"; print "1. Celsius to Fahrenheit.\n"; print "2. Fahrenheit to Celsius. \n"; print "3. Exit\n"; print "*********************************************\n\n"; print "Enter a choice (1-3): "; chomp ($choice =); if(&IsNumeric($choice) == 0) { $choice = 0; } while ($choice != 3) { ################################### ## Do conversion from C to F ################################### if ($choice == 1) { print "\nEnter a Temperature: "; chomp ($cel = ); $fahr = ($cel * (9 / 5)) + 32; ############################### ## Format to one decimal ############################### $fahr = sprintf("%.1f", $fahr); print "$cel degrees Celsius = "; print "$fahr degrees Fahrenheit\n"; } ################################### ## Do conversion from F to C ################################### elsif ($choice == 2) { print "\nEnter a Temperature: "; chomp ($fahr = ); $cel = ($fahr - 32) * 5 / 9; ############################### ## Format to one decimal ############################### $cel = sprintf("%.1f", $cel); print "$fahr degrees Fahrenheit = "; print "$cel degrees Celsius\n"; } ################################### ## Display Error Message ################################### else { print "\nYou entered and invalid choice please choose a choice from the menu.\n\n"; } print "\nEnter a Choice (1-3): "; chomp ($choice = ); if(&IsNumeric($choice) == 0) { $choice = 0; } } ################################################################## ## Sub Name: IsNumeric. ## Description: This sub validates the input to check to see if ## the input is a Numeric value ## Example: 100, 1,000, $10.00, and 14.00 are valid inputs. ################################################################## sub IsNumeric { my $InputString = shift; if ($InputString !~ /^[0-9|.|,]*$/) { return 0; } else { return 1; } }
##################################################### # Big Int Random number generator # # Sidrit Trandafili# # sidrit (at) hotmail (dot) com# # -Generates random ints with more than 16 digits# # with no scientific notation # # -works really quick until 350.000 digits # # then depends from your PC# ## ##################################################### use Math::BigInt; sub create_number{ $length=$_[0]; for($count=0;$count< int($length/16);$count++){ $lower=1; for($i=0;$i<15;$i++){ $lower=$lower."0";} $higher=9; for($j=0;$j<15;$j++){ $higher=$higher."9";} $random = new Math::BigInt int(rand( $higher-$lower+1 ) ) + $lower; $num=$num.$random; } if($length%16 != 0){ $lower=1; for($i=0;$i<$length-(16*int($length/16))-1;$i++){ $lower=$lower."0";} $higher=9; for($j=0;$j<$length-(16*int($length/16))-1;$j++){ $higher=$higher."9";} $random = new Math::BigInt int(rand( $higher-$lower+1 ) ) + $lower; $num=$num.$random;} return $num; } sub usage{ print "\nUsage: big_random_int.pl number_length\n"; } if(@ARGV !=1){ &usage; } elsif(@ARGV == 1){ $length0=$ARGV[0]; if($length0<16){ &usage; } elsif($length0>16){ $output=&create_number($length0); if(length($output)==$length0){ print $output; } } }
#Base converter my ($sbase,$tbase,$sample)=@ARGV; $sbase= get_in( 'Enter the sample base:') unless($sbase ); $tbase= get_in( 'Enter base to convert to:') unless($tbase ); if($sample) { print convert_base($sbase,$sample,$tbase),"\n"; } else { while() { chomp; print convert_base($sbase,$_,$tbase),"\n"; } } sub convert_base { my ($sbase,$sample,$tbase)=@_; my @num=split(//,$sample); my ($num,$i,@res,@symb,%base); foreach(0..9,A..Z,a..z) { push @symb,$_; } foreach(0..@symb-1) { $base{$_}=$symb[$_]; } @num=reverse(@num); foreach(0..@num-1) { #print "Digit $_ (" . $num[$_] . ") is "; $t=$base{$num[$_]} * expn($sbase,$_); $t=$base{$num[$_]} if($_==0); #print $t . "\n"; $num+=$t; } #print "Base is $num\n"; while(expn($tbase,$i)<=$num) { $i++; } $i--; foreach(0..$i) { $_=$i-$_; $res[$_]='0'; while($num>=expn($tbase,$_)) { $num-=expn($tbase,$_); $res[$_]++; } } foreach(@res) { $_=$symb[$_]; } @res=reverse(@res); return join('',@res); } sub expn { my ($num,$expn)=@_; my $n=1; foreach(1..$expn) { $n*=$num; } return $n; } sub get_in { print shift() . ' '; my $data= ; chomp $data; return $data; }
#!/usr/bin/perl # fib1.pl use warnings; use strict; sub fibonacci1 { my ($count, $aref) = @_; unless ($aref) { # first call - initialize $aref = [1,1]; $count -= scalar(@{$aref}); } $aref = [1,1] unless $aref; if ($count--) { my $next = $aref->[-1] + $aref->[-2]; push @{$aref}, $next; return fibonacci1($count, $aref); } else { return wantarray?@{$aref}: $aref->[-1]; } } # calculate 10th element of standard Fibonacci sequence print scalar(fibonacci1(10)), "\n"; # calculate 10th element beyond sequence starting 2, 4 print scalar(fibonacci1(10, [2, 4])), "\n"; # return first ten elements of standard Fibonacci sequence my @sequence = fibonacci1(10); print "Sequence: @sequence \n";
#!/usr/bin/perl # fib2.pl use warnings; use strict; sub fibonacci2 { my ($count, $internal) = @_; if ($count <= 2) { # we know the answer already return $internal?[1,1]: 1; } else { # call ourselves to determine previous two elements my $result = fibonacci2($count -1, 'internal'); # now we can calculate our element my $next = $result->[-1] + $result->[-2]; if ($internal) { push @{$result}, $next; return $result; } else { return $next; } } } foreach (1..20) { print "Element $_ is ", fibonacci2($_), "\n"; }
#!/usr/bin/perl # fib3.pl use warnings; use strict; sub fibonacci3 { my ($count, $aref) = @_; unless ($aref) { # first call - initialize $aref = [1,1]; $count -= scalar(@{$aref}); } if ($count--) { my $next = $aref->[-1] + $aref->[-2]; push @{$aref}, $next; @_ = ($count, $aref); goto &fibonacci3; } else { return wantarray?@{$aref}:$aref->[-1]; } } # calculate 1000th element of standard Fibonacci sequence print scalar(fibonacci3(1000)), "\n";
#!/usr/bin/perl -w ############################################### ## A random Number generator list. ## This script will generato a list of ## random numbers that are not repeated ## in the list. Very useful in generating ## lotto numbers or anything else that ## you can not have repetitive numbers ############################################### use strict; use integer; ######################################## ## Configuration: ## @Numbers = a valid range of numbers ## $Limit= a count of how many random ##to return. This includes 0, so if ##you put 20, 21 numbers will be ##returned in the list. ######################################## my @Numbers = 1..49; my $Limit = 20; my @list = (); print "\n****************************************************\n"; print "***** This is a Random number generator script *****\n"; print "****************************************************\n\n\n"; for(my $i = 0; $i <= $Limit; $i++) { my $intRand = int(rand(@Numbers))+1; ######################################### ## Add the first number to the list, and ## then compare all remaining numbers ## with those numbers already in the list ######################################### if ($i == 0) { $list[$i] = $intRand; } else { for (my $j = 0; $j<$i; $j++) { while ($intRand == $list[$j]) { ######################################### ## The random number has already been ## added to the list. Generate a new ## Random number and set the $j counter ## to -1 so it starts over to see if the ## newly generated number is in the list ######################################### $intRand = int(rand(@Numbers))+1; $j = -1; } ######################################### ## We just found another random number ## that has not been added to the list ## yet, so we are adding it in now. ######################################### $list[$i] = $intRand; } } } print "****************************************************\n"; print "** This is the list of ". ($Limit+1) ." random numbers chosen\n"; print "** by the system in the order they were chosen\n"; print "****************************************************\n\n"; for (my $i = 0; $i <=$Limit; $i++) { print "$list[$i] "; } print "\n\n\n"; ########################################## ## Now Sort the Numeric list of numbers ########################################## my @SortedList = sort {$a <=> $b} @list; print "****************************************************\n"; print "** This is the Sorted list of ". ($Limit+1) ." random\n"; print "** numbers chosen by the system\n"; print "****************************************************\n\n"; for (my $i = 0; $i <=$Limit; $i++) { print "$SortedList[$i] "; } print "\n\n\n";