From ff2eb9d216c820d3549a95a6f3ecb5edc0cdcd47 Mon Sep 17 00:00:00 2001 From: Steven Wilson Date: Mon, 12 Aug 2019 16:28:34 +0100 Subject: add solution for week 21 task 1 --- challenge-021/steven-wilson/perl5/ch-01.pl | 32 ++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 challenge-021/steven-wilson/perl5/ch-01.pl diff --git a/challenge-021/steven-wilson/perl5/ch-01.pl b/challenge-021/steven-wilson/perl5/ch-01.pl new file mode 100644 index 0000000000..a985076177 --- /dev/null +++ b/challenge-021/steven-wilson/perl5/ch-01.pl @@ -0,0 +1,32 @@ +#!/usr/bin/env perl +# Author: Steven Wilson +# Date: 2019-08-12 +# Week: 021 +# +# Task #1 +# +# Write a script to calculate the value of e, also known as Euler’s +# number and Napier’s constant. Please checkout wiki page for more +# information. +# https://en.wikipedia.org/wiki/E_(mathematical_constant) + +use strict; +use warnings; +use feature qw/ say /; +use Math::BigFloat; + +my $number = 0; +my $factorial = 1; +my $e = Math::BigFloat->new( 1 / $factorial ); +my $previous_e = 0; + +while ( $e != $previous_e ) { + # say $e; + $number++; + $factorial *= $number; + $previous_e = $e->copy(); + $e += ( 1 / $factorial ); +} + +my $dp = 50; +say "value of e to $dp decimal places: ", $e->fround($dp); -- cgit