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

m
(New post in addition to an existing post which was retained. The existing post used, by its author's admission, a very inefficient algorithm.)
 
(5 intermediate revisions by 3 users not shown)
Line 717:
5415589 101012010210101(3) 10100101010001010100101(2)
90396755477 22122022220102222022122(3) 1010100001100000100010000011000010101(2)</pre>
 
=={{header|EasyLang}}==
<syntaxhighlight>
fastfunc ispalin2 n .
m = n
while m > 0
x = x * 2 + m mod 2
m = m div 2
.
if n = x
return 1
.
.
fastfunc reverse3 n .
while n > 0
r = r * 3 + n mod 3
n = n div 3
.
return r
.
func$ itoa n b .
if n > 0
return itoa (n div b) b & n mod b
.
.
proc main . .
print "0 0(2) 0(3)"
print "1 1(2) 1(3)"
pow3 = 3
while 1 = 1
for i = pow3 / 3 to pow3 - 1
# assumption that the middle digit must be 1
n = (i * 3 + 1) * pow3 + reverse3 i
if ispalin2 n = 1
print n & " " & itoa n 2 & "(2) " & itoa n 3 & "(3)"
cnt += 1
if cnt = 6 - 2
return
.
.
.
pow3 *= 3
.
.
main
</syntaxhighlight>
{{out}}
<pre>
0 0(2) 0(3)
1 1(2) 1(3)
6643 1100111110011(2) 100010001(3)
1422773 101011011010110110101(2) 2200021200022(3)
5415589 10100101010001010100101(2) 101012010210101(3)
90396755477 1010100001100000100010000011000010101(2) 22122022220102222022122(3)
</pre>
 
=={{header|Elixir}}==
Line 1,265 ⟶ 1,320:
long powerOf3 = 1;
long sum = 0;
for ( int i = 0; i < ternary.length(); i++ ) { // Right part of a palindrome is the mirror image of left part
if ( ternary.charAt(i) > '0' ) {
sum += ( ternary.charAt(i) - '0' ) * powerOf3;
Line 3,434 ⟶ 3,489:
{{libheader|Wren-fmt}}
Just the first 6 palindromes as the 7th is too large for Wren to process without resorting to BigInts.
<syntaxhighlight lang="ecmascriptwren">import "./fmt" for Fmt
 
var isPalindrome2 = Fn.new { |n|
2,063

edits