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.

Comments on this post are now closed. If you have particular grounds for adding a late comment, comment on a more recent post quoting the URL of this one.

Tags 1920s 1930s 1940s 1950s 1960s 1970s 1980s 1990s 2000s 2010s 3d printing action advent of code aeronautics aikakirja anecdote animation anime army astronomy audio audio tech aviation base commerce battletech beer boardgaming book of the week bookmonth chain of command children chris chronicle church of no redeeming virtues cold war comedy computing contemporary cornish smuggler cosmic encounter coup covid-19 crime cthulhu eternal cycling dead of winter doctor who documentary drama driving drone ecchi economics espionage essen 2015 essen 2016 essen 2017 essen 2018 essen 2019 essen 2022 existential risk falklands war fandom fanfic fantasy feminism film firefly first world war flash point flight simulation food garmin drive gazebo genesys geocaching geodata gin gkp gurps gurps 101 gus harpoon historical history horror hugo 2014 hugo 2015 hugo 2016 hugo 2017 hugo 2018 hugo 2019 hugo 2020 hugo 2022 hugo-nebula reread in brief avoid instrumented life javascript julian simpson julie enfield kickstarter kotlin learn to play leaving earth linux liquor lovecraftiana lua mecha men with beards mpd museum music mystery naval noir non-fiction one for the brow opera parody paul temple perl perl weekly challenge photography podcast politics postscript 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 ruby rust science fiction scythe second world war security shipwreck simutrans smartphone south atlantic war squaddies stationery steampunk stuarts suburbia superheroes suspense television the resistance the weekly challenge 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