diff options
| author | Noud Aldenhoven <noud.aldenhoven@gmail.com> | 2020-08-09 14:19:34 +0200 |
|---|---|---|
| committer | Noud Aldenhoven <noud.aldenhoven@gmail.com> | 2020-08-09 14:19:34 +0200 |
| commit | 5b6e0ebcb546d319b9fc66d8bb3a40a1785eefee (patch) | |
| tree | 345910451ca68757bf8332bec11a99ceb3c3014e /challenge-072 | |
| parent | 7b445bf644ffd938b6981830e1b705fedf5c0bd3 (diff) | |
| download | perlweeklychallenge-club-5b6e0ebcb546d319b9fc66d8bb3a40a1785eefee.tar.gz perlweeklychallenge-club-5b6e0ebcb546d319b9fc66d8bb3a40a1785eefee.tar.bz2 perlweeklychallenge-club-5b6e0ebcb546d319b9fc66d8bb3a40a1785eefee.zip | |
Solution to challenge 072 task 1 and 2 in Raku by Noud
Diffstat (limited to 'challenge-072')
| -rw-r--r-- | challenge-072/noud/raku/ch-1.p6 | 35 | ||||
| -rw-r--r-- | challenge-072/noud/raku/ch-2.p6 | 36 | ||||
| -rw-r--r-- | challenge-072/noud/raku/input.txt | 100 |
3 files changed, 171 insertions, 0 deletions
diff --git a/challenge-072/noud/raku/ch-1.p6 b/challenge-072/noud/raku/ch-1.p6 new file mode 100644 index 0000000000..a857150e48 --- /dev/null +++ b/challenge-072/noud/raku/ch-1.p6 @@ -0,0 +1,35 @@ +# You are given a positive integer $N (<= 10). +# +# Write a script to print number of trailing zeroes in $N!. +# Example 1 +# Input: $N = 10 +# Output: 2 as $N! = 3628800 has 2 trailing zeroes +# +# Example 2 +# Input: $N = 7 +# Output: 1 as $N! = 5040 has 1 trailing zero +# +# Example 3 +# Input: $N = 4 +# Output: 0 as $N! = 24 has 0 trailing zero + +# The only thing you have to do is count the prime factors 2 * 5 in the +# factorial. Because half of the natural numbers are even there will be plenty +# factors of 2 in the factorial. Hence the only thing we need to count are the +# prime factors of 5. We have an extra factor of 5 when n is: +# 5, 10, 15, 20, 25, ... +# Then we get another factor of 5 when n is: +# 25, 50, 75, 100, 125, ... +# Another with each multiple of 5^3, etc. Thefore the number of factors of 5 +# in n factorial is: +# floor(n / 5) + floor(n / 5^2) + floor(n / 5^3) + ... + +sub trailing-zeros($n) { + [+] (floor($n / 5**$_) for 1..floor($n**(1/5) + 1)); +} + +trailing-zeros(125).say; +trailing-zeros(25).say; +trailing-zeros(10).say; +trailing-zeros(7).say; +trailing-zeros(4).say; diff --git a/challenge-072/noud/raku/ch-2.p6 b/challenge-072/noud/raku/ch-2.p6 new file mode 100644 index 0000000000..9f5759fcde --- /dev/null +++ b/challenge-072/noud/raku/ch-2.p6 @@ -0,0 +1,36 @@ +# 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. +# Example +# Input: +# +# $ cat input.txt +# L1 +# L2 +# L3 +# L4 +# ... +# ... +# ... +# ... +# L100 +# +# $A = 4 and $B = 12 +# +# Output: +# +# L4 +# L5 +# L6 +# L7 +# L8 +# L9 +# L10 +# L11 +# L12 + +sub lines-range($filename, $a, $b) { + $filename.IO.lines[($a-1)..($b-1)]; +} + +.say for lines-range("input.txt", 4, 12); diff --git a/challenge-072/noud/raku/input.txt b/challenge-072/noud/raku/input.txt new file mode 100644 index 0000000000..e5a15512e0 --- /dev/null +++ b/challenge-072/noud/raku/input.txt @@ -0,0 +1,100 @@ +L1 +L2 +L3 +L4 +L5 +L6 +L7 +L8 +L9 +L10 +L11 +L12 +L13 +L14 +L15 +L16 +L17 +L18 +L19 +L20 +L21 +L22 +L23 +L24 +L25 +L26 +L27 +L28 +L29 +L30 +L31 +L32 +L33 +L34 +L35 +L36 +L37 +L38 +L39 +L40 +L41 +L42 +L43 +L44 +L45 +L46 +L47 +L48 +L49 +L50 +L51 +L52 +L53 +L54 +L55 +L56 +L57 +L58 +L59 +L60 +L61 +L62 +L63 +L64 +L65 +L66 +L67 +L68 +L69 +L70 +L71 +L72 +L73 +L74 +L75 +L76 +L77 +L78 +L79 +L80 +L81 +L82 +L83 +L84 +L85 +L86 +L87 +L88 +L89 +L90 +L91 +L92 +L93 +L94 +L95 +L96 +L97 +L98 +L99 +L100 |
