aboutsummaryrefslogtreecommitdiff
path: root/challenge-104
diff options
context:
space:
mode:
authorCris-HD <crisn7@hotmail.com>2021-03-21 16:52:56 +0100
committerCris-HD <crisn7@hotmail.com>2021-03-21 16:52:56 +0100
commitedcf3627d22b2a8b3a08f5ccc792be8f86a66c39 (patch)
treefb6e24b360a71d4f23acddb4db44aeba7972eafc /challenge-104
parente9ea0d8d7f3b84d7a6c204e8fa79f91cd1df7627 (diff)
downloadperlweeklychallenge-club-edcf3627d22b2a8b3a08f5ccc792be8f86a66c39.tar.gz
perlweeklychallenge-club-edcf3627d22b2a8b3a08f5ccc792be8f86a66c39.tar.bz2
perlweeklychallenge-club-edcf3627d22b2a8b3a08f5ccc792be8f86a66c39.zip
Added challenge 104 solution
Diffstat (limited to 'challenge-104')
-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