Jump to content

Find palindromic numbers in both binary and ternary bases: Difference between revisions

Rename Perl 6 -> Raku, alphabetize, minor clean-up
(Add Swift)
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 539:
Real: 00:23:09.114, CPU: 00:23:26.430, GC gen0: 209577, gen1: 1
</pre>
 
=={{header|Factor}}==
This implementation uses the methods for reducing the search space discussed in the Ruby example.
Line 1,310 ⟶ 1,311:
5415589 101012010210101 10100101010001010100101
90396755477 22122022220102222022122 1010100001100000100010000011000010101</pre>
 
=={{header|Perl 6}}==
Instead of searching for numbers that are palindromes in one base then checking the other, generate palindromic trinary numbers directly, then check to see if they are also binary palindromes (with additional simplifying constraints as noted in other entries). Outputs the list in decimal, binary and trinary.
 
<lang perl6>constant palindromes = 0, 1, |gather for 1 .. * -> $p {
my $pal = $p.base(3);
my $n = :3($pal ~ '1' ~ $pal.flip);
next if $n %% 2;
my $b2 = $n.base(2);
next if $b2.chars %% 2;
next unless $b2 eq $b2.flip;
take $n;
 
printf "%d, %s, %s\n", $_, .base(2), .base(3) for palindromes[^6];</lang>
{{out}}
<pre>0, 0, 0
1, 1, 1
6643, 1100111110011, 100010001
1422773, 101011011010110110101, 2200021200022
5415589, 10100101010001010100101, 101012010210101
90396755477, 1010100001100000100010000011000010101, 22122022220102222022122</pre>
 
=={{header|Phix}}==
Line 1,673 ⟶ 1,652:
90396755477 "1010100001100000100010000011000010101" "22122022220102222022122"</pre>
 
=={{Headerheader|Python}}==
===Imperative===
<lang python>from itertools import islice
Line 1,993 ⟶ 1,972:
5: 5415589_10 101012010210101_3 10100101010001010100101_2
6: 90396755477_10 22122022220102222022122_3 1010100001100000100010000011000010101_2</pre>
 
=={{header|Perl 6Raku}}==
(formerly Perl 6)
Instead of searching for numbers that are palindromes in one base then checking the other, generate palindromic trinary numbers directly, then check to see if they are also binary palindromes (with additional simplifying constraints as noted in other entries). Outputs the list in decimal, binary and trinary.
 
<lang perl6>constant palindromes = 0, 1, |gather for 1 .. * -> $p {
my $pal = $p.base(3);
my $n = :3($pal ~ '1' ~ $pal.flip);
next if $n %% 2;
my $b2 = $n.base(2);
next if $b2.chars %% 2;
next unless $b2 eq $b2.flip;
take $n;
 
printf "%d, %s, %s\n", $_, .base(2), .base(3) for palindromes[^6];</lang>
{{out}}
<pre>0, 0, 0
1, 1, 1
6643, 1100111110011, 100010001
1422773, 101011011010110110101, 2200021200022
5415589, 10100101010001010100101, 101012010210101
90396755477, 1010100001100000100010000011000010101, 22122022220102222022122</pre>
 
=={{header|REXX}}==
Line 2,352 ⟶ 2,354:
---
17 palindromes found.</pre>
 
=={{header|Scheme}}==
<lang scheme>(import (scheme base)
10,333

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.