diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2021-06-24 19:37:00 +0100 |
|---|---|---|
| committer | Paulo Custodio <pauloscustodio@gmail.com> | 2021-06-24 19:37:00 +0100 |
| commit | cf638c1ad15fad8e756291ea904de1547d286592 (patch) | |
| tree | 713e1ef6ae5437390cb23a42667f6ef72d0a4229 | |
| parent | cffacaf785da1db6527109548d555eb990071795 (diff) | |
| download | perlweeklychallenge-club-cf638c1ad15fad8e756291ea904de1547d286592.tar.gz perlweeklychallenge-club-cf638c1ad15fad8e756291ea904de1547d286592.tar.bz2 perlweeklychallenge-club-cf638c1ad15fad8e756291ea904de1547d286592.zip | |
Add bc solution to challenge 003
| -rw-r--r-- | challenge-003/paulo-custodio/bc/ch-1.bc | 55 | ||||
| -rw-r--r-- | challenge-006/paulo-custodio/bc/ch-2.bc | 2 |
2 files changed, 56 insertions, 1 deletions
diff --git a/challenge-003/paulo-custodio/bc/ch-1.bc b/challenge-003/paulo-custodio/bc/ch-1.bc new file mode 100644 index 0000000000..119c7557f5 --- /dev/null +++ b/challenge-003/paulo-custodio/bc/ch-1.bc @@ -0,0 +1,55 @@ +#!/usr/bin/bc -ql + +/* +Challenge 003 + +Challenge #1 +Create a script to generate 5-smooth numbers, whose prime divisors are less or +equal to 5. They are also called Hamming/Regular/Ugly numbers. For more +information, please check this wikipedia. +*/ + +scale = 0 + +num = read() + +define min(a,b) { + if (a < b) { return a; } else { return b; } +} + +define min3(a,b,c) { + return min(a,min(b,c)); +} + +q2[0] = 1; q2size = 1 +q3[0] = 1; q3size = 1 +q5[0] = 1; q5size = 1 + +for (i = 0; i < num; i++) { + /* next hamming: get smallest of the queue heads */ + h = min3(q2[0], q3[0], q5[0]) + h + + /* shift used multiples */ + if (h == q2[0]) { + for (j = 1; j < q2size; j++) + q2[j-1] = q2[j] + q2size = q2size-1 + } + if (h == q3[0]) { + for (j = 1; j < q3size; j++) + q3[j-1] = q3[j] + q3size = q3size-1 + } + if (h == q5[0]) { + for (j = 1; j < q5size; j++) + q5[j-1] = q5[j] + q5size = q5size-1 + } + + /* push next multiples */ + q2[q2size] = 2*h; q2size = q2size+1 + q3[q3size] = 3*h; q3size = q3size+1 + q5[q5size] = 5*h; q5size = q5size+1 +} +quit diff --git a/challenge-006/paulo-custodio/bc/ch-2.bc b/challenge-006/paulo-custodio/bc/ch-2.bc index 631718706d..3fa72be981 100644 --- a/challenge-006/paulo-custodio/bc/ch-2.bc +++ b/challenge-006/paulo-custodio/bc/ch-2.bc @@ -5,4 +5,4 @@ ex=pi*sqrt(163) rk=e(ex) scale=12 rk/1 -quit
\ No newline at end of file +quit |
