Peter’s solutions: week 104 — 15 March 2021
THE WEEKLY CHALLENGE
FUSC and NIM
Write a script to generate first 50 members of the FUSC Sequence. Please refer to the OEIS for more information.
The sequence is defined as below:
fusc(0) = 0 fusc(1) = 1 fusc(2) = 1 fusc(3) = 2 fusc(4) = 1 ...
Stern's diatomic series also known as fusc, is a series of interest in the field of number theory, although I have not found any pratical use for it in the documentation.
It is easily computed using the instructions in the task statement above, and that's what I did.
#!/usr/bin/perl # Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge use v5.26; # The Weekly Challenge - 2021-03-15 use utf8; # Week 104 - task 1 - Fusc sequence use warnings; # Peter Campbell Smith binmode STDOUT, ':utf8'; use Encode; fusc_sequence(50); sub fusc_sequence { my ($stop, @fusc); # initialise $stop = $_[0]; # calculate per rules $fusc[0] = 0; $fusc[1] = 1; $fusc[$_] = $_ & 1 ? $fusc[($_ - 1) / 2] + $fusc[($_ + 1) / 2] : $fusc[$_ / 2] for 2 .. $stop - 1; # show results nicely say qq[\nInput: $stop]; say qq[Output: ]; for (0 .. $stop - 1) { print sprintf('fusc(%2d) = %2d', $_, $fusc[$_]) . ($_ < $stop - 1 ? ', ' : ''); say '' if ($_ + 1) % 5 == 0; } }
13 lines of code
Completed after the closing date and not submitted to GitHub
Input: 50 Output: fusc( 0) = 0, fusc( 1) = 1, fusc( 2) = 1, fusc( 3) = 2, fusc( 4) = 1, fusc( 5) = 3, fusc( 6) = 2, fusc( 7) = 3, fusc( 8) = 1, fusc( 9) = 4, fusc(10) = 3, fusc(11) = 5, fusc(12) = 2, fusc(13) = 5, fusc(14) = 3, fusc(15) = 4, fusc(16) = 1, fusc(17) = 5, fusc(18) = 4, fusc(19) = 7, fusc(20) = 3, fusc(21) = 8, fusc(22) = 5, fusc(23) = 7, fusc(24) = 2, fusc(25) = 7, fusc(26) = 5, fusc(27) = 8, fusc(28) = 3, fusc(29) = 7, fusc(30) = 4, fusc(31) = 5, fusc(32) = 1, fusc(33) = 6, fusc(34) = 5, fusc(35) = 9, fusc(36) = 4, fusc(37) = 11, fusc(38) = 7, fusc(39) = 10, fusc(40) = 3, fusc(41) = 11, fusc(42) = 8, fusc(43) = 13, fusc(44) = 5, fusc(45) = 12, fusc(46) = 7, fusc(47) = 9, fusc(48) = 2, fusc(49) = 9
Any content of this website which has been created by Peter Campbell Smith is in the public domain