aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-03-21 19:27:34 +0100
committerGitHub <noreply@github.com>2021-03-21 19:27:34 +0100
commit99ef82fb7c7313a2084680fffc16ff5acd3a17f4 (patch)
tree07862a7a9aef2af41e239c44adb9c090578dd651
parent98b1f95acd154831de603b855f851a9b70fada8d (diff)
parentedcf3627d22b2a8b3a08f5ccc792be8f86a66c39 (diff)
downloadperlweeklychallenge-club-99ef82fb7c7313a2084680fffc16ff5acd3a17f4.tar.gz
perlweeklychallenge-club-99ef82fb7c7313a2084680fffc16ff5acd3a17f4.tar.bz2
perlweeklychallenge-club-99ef82fb7c7313a2084680fffc16ff5acd3a17f4.zip
Merge pull request #3751 from Cris-HD/branch-for-challenge-104
Added challenge 104 solution
-rw-r--r--challenge-104/cristian-heredia/perl/ch_1.pl53
-rw-r--r--challenge-104/cristian-heredia/python/ch_1.py41
2 files changed, 94 insertions, 0 deletions
diff --git a/challenge-104/cristian-heredia/perl/ch_1.pl b/challenge-104/cristian-heredia/perl/ch_1.pl
new file mode 100644
index 0000000000..d80bae6248
--- /dev/null
+++ b/challenge-104/cristian-heredia/perl/ch_1.pl
@@ -0,0 +1,53 @@
+=begin
+
+TASK #1 › FUSC Sequence
+Submitted by: Mohammad S Anwar
+Write a script to generate first 50 members of FUSC Sequence. Please refer to OEIS for more information.
+
+The sequence defined as below:
+
+ fusc(0) = 0
+ fusc(1) = 1
+ for n > 1:
+ when n is even: fusc(n) = fusc(n / 2),
+ when n is odd: fusc(n) = fusc((n-1)/2) + fusc((n+1)/2)
+
+=end
+=cut
+
+use strict;
+use warnings;
+use Data::Dumper;
+
+#Create hash: key (numbers 0-max), value (result fusc)
+my %fusc = (0, 0, 1, 1);
+my $n = 2;
+my $max = 50;
+my $result = '';
+
+sequenceFusc();
+printSequence();
+
+sub sequenceFusc {
+
+ while ($n <= $max) {
+ #if is even:
+ if ($n % 2 == 0) {
+ $fusc{$n} = $fusc{$n/2};
+ }
+ else {
+ $fusc{$n} = $fusc{($n-1)/2} + $fusc{($n+1)/2};
+ }
+ $n++;
+ }
+}
+
+sub printSequence {
+ $n = 0;
+ while ($n <= $max) {
+ $result = $result."$fusc{$n}, ";
+ $n++;
+ }
+ $result =~ s/,\s+$//;
+ print "$result\n";
+}
diff --git a/challenge-104/cristian-heredia/python/ch_1.py b/challenge-104/cristian-heredia/python/ch_1.py
new file mode 100644
index 0000000000..eb88e15307
--- /dev/null
+++ b/challenge-104/cristian-heredia/python/ch_1.py
@@ -0,0 +1,41 @@
+'''
+
+TASK #1 › FUSC Sequence
+Submitted by: Mohammad S Anwar
+Write a script to generate first 50 members of FUSC Sequence. Please refer to OEIS for more information.
+
+The sequence defined as below:
+
+ fusc(0) = 0
+ fusc(1) = 1
+ for n > 1:
+ when n is even: fusc(n) = fusc(n / 2),
+ when n is odd: fusc(n) = fusc((n-1)/2) + fusc((n+1)/2)
+
+'''
+
+#Create hash: key (numbers 0-max), value (result fusc)
+fusc = {0: 0, 1: 1}
+maxi = 50
+
+def sequenceFusc():
+ n = 2
+ while (n <= maxi):
+ #if is even:
+ if (n % 2 == 0):
+ fusc[n] = fusc[n/2]
+ else:
+ fusc[n] = fusc[(n-1)/2] + fusc[(n+1)/2]
+ n += 1
+
+def printSequence():
+ n = 0
+ result = ''
+ while (n <= maxi):
+ result += "{}, ".format(fusc[n])
+ n += 1
+ result = result[:-2]
+ print(result)
+
+sequenceFusc()
+printSequence() \ No newline at end of file