DEV Community

Simon Green
Simon Green

Posted on

Weekly Challenge 150

Challenge, My solutions

TASK #1 › Fibonacci Words

Task

You are given two strings having same number of digits, $a and $b.

Write a script to generate Fibonacci Words by concatenation of the previous two strings. Finally print 51st digit of the first term having at least 51 digits.

My solution

So there is probably some numbers theory about determining which character is the 51st character in a Fibonacci word, but given the small size we are dealing with, it's just easier to do the calculation.

After some basic error checking (we have two strings of the same size), I loop (at least once) adding the two last strings together until we have a string at least 51 characters long. Then I get the fifty first character and display it.

Examples

$ ./ch-1.py 1234 5678
7

$ ./ch-1.pl 1234 5678
7
Enter fullscreen mode Exit fullscreen mode

TASK #2 › Square-free Integer

Task

Write a script to generate all square-free integers <= 500.

In mathematics, a square-free integer is an integer which is divisible by no perfect square other than 1. That is, its prime factorization has exactly one factor for each prime that appears in it. For example, 10 = 2 × 5 is square-free, but 18 = 2 × 3 × 3 is not, because 18 is divisible by 9 = 32.

My solution

There is usually a fine line between optimization, and over-optimization, especially when dealing with small numbers. Hopefully I have the right balance here. There are 21 squares between 2 and 500. Of those, only 8 are from primenumbers. The rest are composites of two other squares. For example, 62 = 22 × 32. Regardless, I put all squares in a list (array in Perl) called sqaures.

I then work through all numbers from 1 to 500 and add them to the solutions list if the number is not perfectly divisible by the values in the squares array.

I probably could have used the lambda call (map in Perl) for even tighter code, but this would reduce the readability of the code.

Example

$ ./ch-2.py 
1, 2, 3, 5, 6, 7, 10, 11, 13, 14, 15, 17, 19, 21, 22, 23, 26, 29, 30, 31, 33, 34, 35, 37, 38, 39, 41, 42, 43, 46, 47, 51, 53, 55, 57, 58, 59, 61, 62, 65, 66, 67, 69, 70, 71, 73, 74, 77, 78, 79, 82, 83, 85, 86, 87, 89, 91, 93, 94, 95, 97, 101, 102, 103, 105, 106, 107, 109, 110, 111, 113, 114, 115, 118, 119, 122, 123, 127, 129, 130, 131, 133, 134, 137, 138, 139, 141, 142, 143, 145, 146, 149, 151, 154, 155, 157, 158, 159, 161, 163, 165, 166, 167, 170, 173, 174, 177, 178, 179, 181, 182, 183, 185, 186, 187, 190, 191, 193, 194, 195, 197, 199, 201, 202, 203, 205, 206, 209, 210, 211, 213, 214, 215, 217, 218, 219, 221, 222, 223, 226, 227, 229, 230, 231, 233, 235, 237, 238, 239, 241, 246, 247, 249, 251, 253, 254, 255, 257, 258, 259, 262, 263, 265, 266, 267, 269, 271, 273, 274, 277, 278, 281, 282, 283, 285, 286, 287, 290, 291, 293, 295, 298, 299, 301, 302, 303, 305, 307, 309, 310, 311, 313, 314, 317, 318, 319, 321, 322, 323, 326, 327, 329, 330, 331, 334, 335, 337, 339, 341, 345, 346, 347, 349, 353, 354, 355, 357, 358, 359, 362, 365, 366, 367, 370, 371, 373, 374, 377, 379, 381, 382, 383, 385, 386, 389, 390, 391, 393, 394, 395, 397, 398, 399, 401, 402, 403, 406, 407, 409, 410, 411, 413, 415, 417, 418, 419, 421, 422, 426, 427, 429, 430, 431, 433, 434, 435, 437, 438, 439, 442, 443, 445, 446, 447, 449, 451, 453, 454, 455, 457, 458, 461, 462, 463, 465, 466, 467, 469, 470, 471, 473, 474, 478, 479, 481, 482, 483, 485, 487, 489, 491, 493, 494, 497, 498, 499
Enter fullscreen mode Exit fullscreen mode

Top comments (0)