RogerBW's Blog

Perl Weekly Challenge 72: factorial zeroes and line ranges 05 August 2020

I’ve been doing the Perl Weekly Challenges. The latest involved finding the trailing zeroes of a factorial and printing a range of lines from a file.

You are given a positive integer $N (<= 10).

Write a script to print number of trailing zeroes in $N!.

There's an easy way and a hard way to do this. Let's do the (computationally) hard way first: actually calculate the factorial (in a library where precision will be retained), and count the zeroes at the end.

use Math::GMP;

sub zerofact {
  my $n=shift;
  my $f=$n->bfac->get_str_gmp(10);
  my $l=0;
  $f =~ /(0+)$/;
  if (defined $1) {
  return $l;

The easier way, which I used in Raku, is not to calculate the factorial at all, but rather to consider that a trailing zero is the result of a pair of (2,5) among the prime factors. And there will always be at least as many twos as fives, so we just need to count the fives. So 0..4 has a result of 0, 5..9 a result of 1, 10..14 2, etc. A slight complication is that at n=25 we add two factors of five, at n=125 three, etc.

sub zerofact($n) {
  my $k=5;
  my $t;
  my $a=0;
  repeat {
  } while ($a>0);
  return $t;

(It's still slower than Perl though.)

You are given a text file name $file and range $A - $B where $A <= $B.

Write a script to display lines range $A and $B in the given file.

Well, that's very straightforward. The only slight cleverness is that we can jump to the end when we hit line number $B.

sub range {
  my $fn=shift;
  my $a=shift;
  my $b=shift;
  my $n=0;
  open I,'<',$fn;
  while (<I>) {
    if ($n > $b) {
    if ($n>=$a) {
      print $_;
  close I;

and Raku is basically identical.

Add A Comment

Your Name
Your Email
Your Comment

Your submission will be ignored if any field is left blank, but your email address will not be displayed. Comments will be processed through markdown.

Tags 1920s 1930s 1940s 1950s 1960s 1970s 1980s 1990s 2000s 2010s 3d printing action aeronautics aikakirja anecdote animation anime army astronomy audio audio tech base commerce battletech beer boardgaming book of the week bookmonth chain of command children chronicle church of no redeeming virtues cold war comedy computing contemporary cornish smuggler cosmic encounter coup covid-19 cycling dead of winter doctor who documentary drama driving drone ecchi economics espionage essen 2015 essen 2016 essen 2017 essen 2018 essen 2019 existential risk falklands war fandom fanfic fantasy feminism film firefly first world war flash point flight simulation food garmin drive gazebo geodata gin gurps gurps 101 harpoon historical history horror hugo 2014 hugo 2015 hugo 2016 hugo 2017 hugo 2018 hugo 2019 hugo 2020 hugo-nebula reread humour in brief avoid instrumented life kickstarter learn to play leaving earth linux lovecraftiana mecha men with beards museum mystery naval non-fiction one for the brow opera perl perl weekly challenge photography podcast politics powers prediction privacy project woolsack pyracantha python quantum rail raku ranting raspberry pi reading reading boardgames social real life restaurant reviews romance rpg a day rpgs science fiction scythe second world war security shipwreck simutrans smartphone south atlantic war squaddies stationery steampunk stuarts suburbia superheroes suspense television the resistance thirsty meeples thriller tin soldier torg toys trailers travel type 26 type 31 type 45 vietnam war war wargaming weather wives and sweethearts writing about writing x-wing young adult
Special All book reviews, All film reviews
Produced by aikakirja v0.1