This is my 21th week participating into the weekly challenge.

3 Sum

Given an array @L of integers. Write a script to find all unique triplets such that a + b + c is same as the given target T . Also make sure a <= b <= c.



Here is wiki page for more information.



Example:

@L = (-25, -10, -7, -3, 2, 4, 8, 10);

One such triplet for target 0 i.e. -10 + 2 + 8 = 0.

For this task I just copied the algorithm from the wiki page for both perl and raku:



sort(S); for i = 0 to n - 2 do a = S[i]; start = i + 1; end = n - 1; while (start < end) do b = S[start] c = S[end]; if (a + b + c == 0) then output a, b, c; start = start + 1; end = end - 1; else if (a + b + c > 0) then end = end - 1; else start = start + 1; end end









Perl 5 solution

#!/usr/bin/perl # Test: ./ch-1.pl use strict; use warnings; use feature qw /say/; my @L = (-25, -10, -7, -3, 2, 4, 8, 10); @L = sort { $a <=> $b } @L; for (my $i = 0; $i < scalar(@L) - 2; $i++) { my $start = $i + 1; my $end = scalar(@L) - 1; while ($start < $end) { if ($L[$i] + $L[$start] + $L[$end] == 0) { say "$L[$i] + $L[$start] + $L[$end] = 0"; $start = $start + 1; $end = $end -1 } elsif ($L[$i] + $L[$start] + $L[$end] > 0) { $end = $end - 1; } else { $start = $start + 1; } } }

Output

-10 + 2 + 8 = 0 -7 + -3 + 10 = 0

Raku solution

# Test: perl6 ch-1.p6 use v6.d; sub MAIN() { my @L = (-25, -10, -7, -3, 2, 4, 8, 10).sort; loop (my $i = 0; $i < @L.elems - 2; $i++) { my $start = $i + 1; my $end = @L.elems - 1; while ($start < $end) { if (@L[$i] + @L[$start] + @L[$end] == 0) { say "@L[$i] + @L[$start] + @L[$end] = 0"; $start = $start + 1; $end = $end -1 } elsif (@L[$i] + @L[$start] + @L[$end] > 0) { $end = $end -1; } else { $start = $start + 1; } } } }

Output

-10 + 2 + 8 = 0 -7 + -3 + 10 = 0

Task 2

Colourful Number

Write a script to display all Colorful Number with 3 digits.

A number can be declare Colorful Number where all the products of consecutive subsets of digit are different.

For example, 263 is a Colorful Number since 2, 6, 3, 2×6, 6×3, 2x6x3 are unique.

For this task I just brute forced the solution, looping through every 3 digit number and doing the calculations.



I made some slight efficiency upgrades by removing the numbers 0 and 1 from the calculations as testing either of these numbers will result in duplications because of their nature.

Update: I mistakenly checked the that the hundredths and ones product was also unique. I modified my solution.

Perl 5 solution

#!/usr/bin/perl # Test: ./ch-2.pl use strict; use warnings; use feature qw /say/; my @solutions; for my $h (2 ... 9) { for my $t (2 .. 9) { for my $o (2 .. 9) { if ( $h * $t != $t * $o && $h * $t != $h * $t * $o && $t * $o != $h * $t * $o ) { push @solutions, "$h$t$o"; } } } } say join ' ', @solutions;

Output

223 224 225 226 227 228 229 233 234 235 236 237 238 239 243 244 245 246 247 248 249 253 254 255 256 257 258 259 263 264 265 266 267 268 269 273 274 275 276 277 278 279 283 284 285 286 287 288 289 293 294 295 296 297 298 299 322 324 325 326 327 328 329 332 334 335 336 337 338 339 342 344 345 346 347 348 349 352 354 355 356 357 358 359 362 364 365 366 367 368 369 372 374 375 376 377 378 379 382 384 385 386 387 388 389 392 394 395 396 397 398 399 422 423 425 426 427 428 429 432 433 435 436 437 438 439 442 443 445 446 447 448 449 452 453 455 456 457 458 459 462 463 465 466 467 468 469 472 473 475 476 477 478 479 482 483 485 486 487 488 489 492 493 495 496 497 498 499 522 523 524 526 527 528 529 532 533 534 536 537 538 539 542 543 544 546 547 548 549 552 553 554 556 557 558 559 562 563 564 566 567 568 569 572 573 574 576 577 578 579 582 583 584 586 587 588 589 592 593 594 596 597 598 599 622 623 624 625 627 628 629 632 633 634 635 637 638 639 642 643 644 645 647 648 649 652 653 654 655 657 658 659 662 663 664 665 667 668 669 672 673 674 675 677 678 679 682 683 684 685 687 688 689 692 693 694 695 697 698 699 722 723 724 725 726 728 729 732 733 734 735 736 738 739 742 743 744 745 746 748 749 752 753 754 755 756 758 759 762 763 764 765 766 768 769 772 773 774 775 776 778 779 782 783 784 785 786 788 789 792 793 794 795 796 798 799 822 823 824 825 826 827 829 832 833 834 835 836 837 839 842 843 844 845 846 847 849 852 853 854 855 856 857 859 862 863 864 865 866 867 869 872 873 874 875 876 877 879 882 883 884 885 886 887 889 892 893 894 895 896 897 899 922 923 924 925 926 927 928 932 933 934 935 936 937 938 942 943 944 945 946 947 948 952 953 954 955 956 957 958 962 963 964 965 966 967 968 972 973 974 975 976 977 978 982 983 984 985 986 987 988 992 993 994 995 996 997 998

Raku solution

# Test: perl6 ch-2.p6 use v6.d; sub MAIN() { my @solutions; for (2 ... 9) -> $h { for (2 .. 9) -> $t { for (2 .. 9) -> $o { if ( $h * $t != $t * $o && $h * $t != $h * $t * $o && $t * $o != $h * $t * $o ) { push @solutions, "$h$t$o"; } } } } say @solutions.join(" "); }

Output

223 224 225 226 227 228 229 233 234 235 236 237 238 239 243 244 245 246 247 248 249 253 254 255 256 257 258 259 263 264 265 266 267 268 269 273 274 275 276 277 278 279 283 284 285 286 287 288 289 293 294 295 296 297 298 299 322 324 325 326 327 328 329 332 334 335 336 337 338 339 342 344 345 346 347 348 349 352 354 355 356 357 358 359 362 364 365 366 367 368 369 372 374 375 376 377 378 379 382 384 385 386 387 388 389 392 394 395 396 397 398 399 422 423 425 426 427 428 429 432 433 435 436 437 438 439 442 443 445 446 447 448 449 452 453 455 456 457 458 459 462 463 465 466 467 468 469 472 473 475 476 477 478 479 482 483 485 486 487 488 489 492 493 495 496 497 498 499 522 523 524 526 527 528 529 532 533 534 536 537 538 539 542 543 544 546 547 548 549 552 553 554 556 557 558 559 562 563 564 566 567 568 569 572 573 574 576 577 578 579 582 583 584 586 587 588 589 592 593 594 596 597 598 599 622 623 624 625 627 628 629 632 633 634 635 637 638 639 642 643 644 645 647 648 649 652 653 654 655 657 658 659 662 663 664 665 667 668 669 672 673 674 675 677 678 679 682 683 684 685 687 688 689 692 693 694 695 697 698 699 722 723 724 725 726 728 729 732 733 734 735 736 738 739 742 743 744 745 746 748 749 752 753 754 755 756 758 759 762 763 764 765 766 768 769 772 773 774 775 776 778 779 782 783 784 785 786 788 789 792 793 794 795 796 798 799 822 823 824 825 826 827 829 832 833 834 835 836 837 839 842 843 844 845 846 847 849 852 853 854 855 856 857 859 862 863 864 865 866 867 869 872 873 874 875 876 877 879 882 883 884 885 886 887 889 892 893 894 895 896 897 899 922 923 924 925 926 927 928 932 933 934 935 936 937 938 942 943 944 945 946 947 948 952 953 954 955 956 957 958 962 963 964 965 966 967 968 972 973 974 975 976 977 978 982 983 984 985 986 987 988 992 993 994 995 996 997 998