Jump to content

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

m
syntax highlighting fixup automation
(Picat constrain solver solution)
m (syntax highlighting fixup automation)
Line 22:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">V digits = ‘0123456789abcdefghijklmnopqrstuvwxyz’
 
F baseN(=num, b)
Line 55:
 
L(pal23) pal_23(6)
print(pal23‘ ’baseN(pal23, 3)‘ ’baseN(pal23, 2))</langsyntaxhighlight>
 
{{out}}
Line 69:
=={{header|Ada}}==
===Simple Technique (Brute Force)===
<langsyntaxhighlight Adalang="ada">with Ada.Text_IO, Base_Conversion;
 
procedure Brute is
Line 95:
end if;
end loop;
end Brute;</langsyntaxhighlight>
{{out}}
<pre> 0: 0(2), 0(3)
Line 111:
The code is then very fast and also very much readable than if we had done the bit manipulations by hand.
 
<langsyntaxhighlight Adalang="ada">with Ada.Text_IO, Ada.Unchecked_Conversion;use Ada.Text_IO;
procedure Palindromic is
type Int is mod 2**64; -- the size of the unsigned values we will test doesn't exceed 64 bits
Line 159:
end loop;
end loop Process_Each_Power_Of_4;
end Palindromic;</langsyntaxhighlight>
{{out}}
On a modern machine, (core i5 for example), this code, compiled with the -O3 and -gnatp options, takes less than 5 seconds to give the seven first palindromes smaller than 2^64.
Line 173:
On my machine, this gets the first five results practically instantaneously and the sixth about eight seconds later.
 
<langsyntaxhighlight lang="applescript">on intToText(int, base) -- Simple version for brevity.
script o
property digits : {int mod base as integer}
Line 243:
end task
 
task()</langsyntaxhighlight>
 
{{output}}
<langsyntaxhighlight lang="applescript">"decimal binary ternary:
0 0 0
1 1 1
Line 252:
1422773 101011011010110110101 2200021200022
5415589 10100101010001010100101 101012010210101
90396755477 1010100001100000100010000011000010101 22122022220102222022122"</langsyntaxhighlight>
 
=={{header|Arturo}}==
{{trans|Ada}}
<langsyntaxhighlight lang="rebol">pal2?: function [n][
digs2: digits.base:2 n
return digs2 = reverse digs2
Line 302:
]
 
pal23</langsyntaxhighlight>
 
{{out}}
Line 315:
=={{header|C}}==
Per the observations made by the Ruby code (which are correct), the numbers must have odd number of digits in base 3 with a 1 at the middle, and must have odd number of digits in base 2.
<langsyntaxhighlight lang="c">#include <stdio.h>
typedef unsigned long long xint;
 
Line 396:
}
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>0 0(2) 0(3)
Line 411:
and then checked to see if they are palindromic in binary.<br/>
The first 6 numbers take about 1/10th of a second. The 7th number takes about 3 and a half minutes.
<langsyntaxhighlight lang="csharp">using System;
using System.Collections.Generic;
using System.Linq;
Line 493:
}
 
}</langsyntaxhighlight>
{{out}}
<pre style="height:30ex;overflow:scroll">
Line 523:
=={{header|Common Lisp}}==
Unoptimized version
<langsyntaxhighlight lang="lisp">(defun palindromep (str)
(string-equal str (reverse str)) )
 
Line 534:
(palindromep (format nil "~3R" i)) )
(format t "n:~a~:* [2]:~B~:* [3]:~3R~%" i)
(incf results) ))</langsyntaxhighlight>
{{out}}
<pre>n:0 [2]:0 [3]:0
Line 546:
=={{header|D}}==
{{trans|C}}
<langsyntaxhighlight lang="d">import core.stdc.stdio, std.ascii;
 
bool isPalindrome2(ulong n) pure nothrow @nogc @safe {
Line 604:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>0 0(3) 0(2)
Line 616:
{{trans|Ruby}}
{{works with|Elixir|1.3}}
<langsyntaxhighlight lang="elixir">defmodule Palindromic do
import Integer, only: [is_odd: 1]
 
Line 643:
end
 
Palindromic.task</langsyntaxhighlight>
{{out}}
<pre> decimal ternary binary
Line 654:
 
=={{header|F_Sharp|F#}}==
<langsyntaxhighlight lang="fsharp">
// Find palindromic numbers in both binary and ternary bases. December 19th., 2018
let fG(n,g)=(Seq.unfold(fun(g,e)->if e<1L then None else Some((g%3L)*e,(g/3L,e/3L)))(n,g/3L)|>Seq.sum)+g+n*g*3L
Seq.concat[seq[0L;1L;2L];Seq.unfold(fun(i,e)->Some (fG(i,e),(i+1L,if i=e-1L then e*3L else e)))(1L,3L)]
|>Seq.filter(fun n->let n=System.Convert.ToString(n,2).ToCharArray() in n=Array.rev n)|>Seq.take 6|>Seq.iter (printfn "%d")
</syntaxhighlight>
</lang>
{{out}}
Finding 6 takes no time.
Line 685:
=={{header|Factor}}==
This implementation uses the methods for reducing the search space discussed in the Ruby example.
<langsyntaxhighlight lang="factor">USING: combinators.short-circuit formatting io kernel lists
lists.lazy literals math math.parser sequences tools.time ;
IN: rosetta-code.2-3-palindromes
Line 710:
] each ;
 
[ main ] time</langsyntaxhighlight>
{{out}}
<pre>The first 6 numbers which are palindromic in both binary and ternary:
Line 744:
and check if they are also binary palindromes using the optimizations which have been noted in some
of the other language solutions :
<langsyntaxhighlight lang="freebasic">' FB 1.05.0 Win64
 
'converts decimal "n" to its ternary equivalent
Line 816:
Print " seconds on i3 @ 2.13 GHz"
Print "Press any key to quit"
Sleep</langsyntaxhighlight>
{{out}}
<pre>The first 6 numbers which are palindromic in both binary and ternary are :
Line 849:
{{trans|C}}
On my modest machine (Intel Celeron @1.6ghz) this takes about 30 seconds to produce the 7th palindrome. Curiously, the C version (GCC 5.4.0, -O3) takes about 55 seconds on the same machine. As it's a faithful translation, I have no idea why.
<langsyntaxhighlight lang="go">package main
 
import (
Line 949:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 991:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">import Data.Char (digitToInt, intToDigit, isDigit)
import Data.List (transpose, unwords)
import Numeric (readInt, showIntAtBase)
Line 1,055:
 
showBase :: Integer -> Integer -> String
showBase base n = showIntAtBase base intToDigit n []</langsyntaxhighlight>
{{Out}}
<pre>Decimal Ternary Binary
Line 1,067:
=={{header|J}}==
'''Solution:'''
<langsyntaxhighlight lang="j">isPalin=: -: |. NB. check if palindrome
toBase=: #.inv"0 NB. convert to base(s) in left arg
filterPalinBase=: ] #~ isPalin@toBase/ NB. palindromes for base(s)
Line 1,087:
end.
y{.res
)</langsyntaxhighlight>
'''Usage:'''
<langsyntaxhighlight lang="j"> find23Palindromes i. 2e6 NB. binary & ternary palindromes less than 2,000,000
0 1 6643 1422773
10 2 3 showBases find23Palindromes getfirst 6 NB. first 6 binary & ternary palindomes
Line 1,097:
1422773 101011011010110110101 2200021200022
5415589 10100101010001010100101 101012010210101
90396755477 1010100001100000100010000011000010101 22122022220102222022122</langsyntaxhighlight>
 
=={{header|Java}}==
This takes a while to get to the 6th one (I didn't time it precisely, but it was less than 2 hours on an i7)
<langsyntaxhighlight lang="java">public class Pali23 {
public static boolean isPali(String x){
return x.equals(new StringBuilder(x).reverse().toString());
Line 1,118:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>0, 0, 0
Line 1,130:
===ES6===
{{Trans|Haskell}}
<langsyntaxhighlight JavaScriptlang="javascript">(() => {
'use strict';
 
Line 1,254:
)))
.map(unwords));
})();</langsyntaxhighlight>
{{Out}}
<pre>Decimal Ternary Binary
Line 1,266:
=={{header|Julia}}==
{{trans|C}}
<langsyntaxhighlight lang="julia">ispalindrome(n, bas) = (s = string(n, base=bas); s == reverse(s))
prin3online(n) = println(lpad(n, 15), lpad(string(n, base=2), 40), lpad(string(n, base=3), 30))
reversebase3(n) = (x = 0; while n != 0 x = 3x + (n %3); n = div(n, 3); end; x)
Line 1,315:
 
printpalindromes(6)
</langsyntaxhighlight>{{out}}
<pre>
Number Base 2 Base 3
Line 1,328:
=={{header|Kotlin}}==
{{trans|FreeBASIC}}
<langsyntaxhighlight lang="scala">// version 1.0.5-2
 
/** converts decimal 'n' to its ternary equivalent */
Line 1,399:
}
while (count < 6)
}</langsyntaxhighlight>
{{out}}
<pre>The first 6 numbers which are palindromic in both binary and ternary are:
Line 1,428:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">palindromify3[n_] :=
Block[{digits},
If[Divisible[n, 3], {},
Line 1,437:
];
base2PalindromeQ[n_] := IntegerDigits[n, 2] === Reverse[IntegerDigits[n, 2]];
Select[Flatten[palindromify3 /@ Range[1000000]], base2PalindromeQ]</langsyntaxhighlight>
 
{{out}}
Line 1,444:
=={{header|Nim}}==
{{trans|Ada}}
<langsyntaxhighlight Nimlang="nim">import bitops, strformat, times
 
#---------------------------------------------------------------------------------------------------
Line 1,520:
let t0 = cpuTime()
findPal23()
echo fmt"\nTime: {cpuTime() - t0:.2f}s"</langsyntaxhighlight>
 
{{out}}
Line 1,534:
 
=={{header|PARI/GP}}==
<langsyntaxhighlight lang="parigp">check(n)={ \\ Check for 2n+1-digit palindromes in base 3
my(N=3^n);
forstep(i=N+1,2*N,[1,2],
Line 1,544:
)
};
print1("0, 1"); for(i=1,11,check(i))</langsyntaxhighlight>
{{out}}
<pre>0, 1, 6643, 1422773, 5415589, 90396755477</pre>
Line 1,550:
=={{header|Perl}}==
{{libheader|ntheory}}
<langsyntaxhighlight lang="perl">use ntheory qw/fromdigits todigitstring/;
 
print "0 0 0\n"; # Hard code the 0 result
Line 1,561:
# Print results (including base 10) if base-2 palindrome
print fromdigits($b2,2)," $b3 $b2\n" if $b2 eq reverse($b2);
}</langsyntaxhighlight>
{{out}}
<pre>0 0 0
Line 1,579:
that turned out noticeably slower.
 
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #000080;font-style:italic;">-- widths and limits for 32/64 bit running (see output below):</span>
Line 1,719:
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">elapsed</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">time</span><span style="color: #0000FF;">()-</span><span style="color: #000000;">t0</span><span style="color: #0000FF;">)</span>
<!--</langsyntaxhighlight>-->
{{out}}
32 bit:
Line 1,749:
=== much simpler version ===
(slightly but not alot faster)
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">to_base</span><span style="color: #0000FF;">(</span><span style="color: #004080;">atom</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">base</span><span style="color: #0000FF;">)</span>
Line 1,796:
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">elapsed</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">time</span><span style="color: #0000FF;">()-</span><span style="color: #000000;">t0</span><span style="color: #0000FF;">)</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 1,811:
=== much faster version ===
Inspired by Scala 😏
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">to_base</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">base</span><span style="color: #0000FF;">)</span>
Line 1,853:
<span style="color: #000000;">center</span><span style="color: #0000FF;">(</span><span style="color: #000000;">to_base</span><span style="color: #0000FF;">(</span><span style="color: #000000;">A</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">],</span><span style="color: #000000;">3</span><span style="color: #0000FF;">),</span><span style="color: #000000;">145</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 1,892:
</pre>
=={{header|Picat}}==
<syntaxhighlight lang="picat">
<lang Picat>
import sat.
to_num(List, Base, Num) =>
Line 1,925:
printf("%w %s %s%n", Y, to_radix_string(Y,2), to_radix_string(Y,3))
end.
</syntaxhighlight>
</lang>
Output:
<pre>0 0 0
Line 1,935:
 
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="picolisp">(de ternary (N)
(if (=0 N)
(cons N)
Line 1,952:
(println N (pack B2) (pack B3))
(inc 'I) )
(inc 'N) ) )</langsyntaxhighlight>
{{out}}
<pre>0 "0" "0"
Line 1,963:
=={{header|Python}}==
===Imperative===
<langsyntaxhighlight lang="python">from itertools import islice
 
digits = "0123456789abcdefghijklmnopqrstuvwxyz"
Line 1,996:
 
for pal23 in islice(pal_23(), 6):
print(pal23, baseN(pal23, 3), baseN(pal23, 2))</langsyntaxhighlight>
{{out}}
<pre>0 0 0
Line 2,007:
===Functional===
{{Works with|Python|3.7}}
<langsyntaxhighlight Pythonlang="python">'''Numbers with palindromic digit strings in both binary and ternary'''
 
from itertools import (islice)
Line 2,181:
# MAIN ---
if __name__ == '__main__':
main()</langsyntaxhighlight>
{{Out}}
<pre> Decimal Binary Ternary
Line 2,193:
 
=={{header|Racket}}==
<langsyntaxhighlight lang="racket">#lang racket
(require racket/generator)
 
Line 2,279:
(map (curryr number->string 2) (for/list ((i 16) (p (in-producer (b-palindromes-generator 2)))) p))
(list "0" "1" "11" "101" "111" "1001" "1111" "10001" "10101" "11011"
"11111" "100001" "101101" "110011" "111111" "1000001")))</langsyntaxhighlight>
{{out}}
<pre> 1: 0_10 0_3 0_2
Line 2,292:
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.
 
<syntaxhighlight lang="raku" perl6line>constant palindromes = 0, 1, |gather for 1 .. * -> $p {
my $pal = $p.base(3);
my $n = :3($pal ~ '1' ~ $pal.flip);
Line 2,302:
}
 
printf "%d, %s, %s\n", $_, .base(2), .base(3) for palindromes[^6];</langsyntaxhighlight>
{{out}}
<pre>0, 0, 0
Line 2,328:
::* &nbsp; convert the decimal numbers to base 3,
::* &nbsp; ensure that the numbers in base 3 are palindromic.
<langsyntaxhighlight lang="rexx">/*REXX program finds numbers that are palindromic in both binary and ternary. */
digs=50; numeric digits digs /*biggest known B2B3 palindrome: 44 dig*/
parse arg maxHits .; if maxHits=='' then maxHits=6 /*use six as a limit.*/
Line 2,357:
if hits>2 then if hits//2 then #=#'0'
if hits<maxHits then return /*Not enough palindromes? Keep looking*/
exit /*stick a fork in it, we're all done. */</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default input of: &nbsp; &nbsp; <tt> 7 </tt>}}
<pre> [1] 0 (decimal), ternary= 0
Line 2,383:
===version 2===
This REXX version takes advantage that the palindromic numbers (in both binary and ternary bases) &nbsp; ''seem'' &nbsp; to only have a modulus nine residue of &nbsp; 1, 5, 7, or 8. &nbsp; With this assumption, the following REXX program is about 25% faster.
<langsyntaxhighlight lang="rexx">/*REXX program finds numbers that are palindromic in both binary and ternary. */
digs=50; numeric digits digs /*biggest known B2B3 palindrome: 44 dig*/
parse arg maxHits .; if maxHits=='' then maxHits=6 /*use six as a limit.*/
Line 2,413:
if hits>2 then if hits//2 then #=#'0'
if hits<maxHits then return /*Not enough palindromes? Keep looking*/
exit /*stick a fork in it, we're all done. */</langsyntaxhighlight>
{{out|output|text=&nbsp; is identical to the 1<sup>st</sup> REXX version.}}
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
# Project: Find palindromic numbers in both binary and ternary bases
 
Line 2,465:
return 0
ok
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,490:
This program constructs base 3 palindromes using the above "rules" and checks if they happen to be binary palindromes.
 
<langsyntaxhighlight lang="ruby">pal23 = Enumerator.new do |y|
y << 0
y << 1
Line 2,505:
n = pal23.next
puts "%2d: %12d %s %s" % [i, n, n.to_s(3).center(25), n.to_s(2).center(39)]
end</langsyntaxhighlight>
{{out}}
<pre> decimal ternary binary
Line 2,518:
===Functional programmed, (tail) recursive===
{{Out}}Best seen running in your browser either by [https://scalafiddle.io/sf/ZYCqm7p/0 ScalaFiddle (ES aka JavaScript, non JVM)] or [https://scastie.scala-lang.org/WIL3oAwYSRy4Kl918u13CA Scastie (remote JVM)].
<langsyntaxhighlight Scalalang="scala">import scala.annotation.tailrec
import scala.compat.Platform.currentTime
 
Line 2,555:
println(s"Successfully completed without errors. [total ${currentTime - executionStartTime} ms]")
 
}</langsyntaxhighlight>
 
===Fastest and high yields (17) solution 😏===
{{Out}}Best seen running in your browser either by [https://scastie.scala-lang.org/en0ZiqDETCuWO6avhTi9YQ Scastie (remote JVM)].
<langsyntaxhighlight Scalalang="scala">import scala.io.Source
 
object FastPalindrome23 extends App {
Line 2,581:
println(s"${count} palindromes found.")
 
}</langsyntaxhighlight>
{{Out}}
<pre>Decimal : 0 , Central binary digit: 0
Line 2,671:
 
=={{header|Scheme}}==
<langsyntaxhighlight lang="scheme">(import (scheme base)
(scheme write)
(srfi 1 lists)) ; use 'fold' from SRFI 1
Line 2,722:
(r-number->string n 3)))
(newline))
(get-series 6))</langsyntaxhighlight>
 
{{out}}
Line 2,734:
=={{header|Sidef}}==
{{trans|Perl}}
<langsyntaxhighlight lang="ruby">var format = "%11s %24s %38s\n"
format.printf("decimal", "ternary", "binary")
format.printf(0, 0, 0)
Line 2,745:
format.printf(Num(b2, 2), b3, b2)
}
}</langsyntaxhighlight>
{{out}}
<pre> decimal ternary binary
Line 2,759:
{{trans|C}}
 
<langsyntaxhighlight lang="swift">import Foundation
 
func isPalin2(n: Int) -> Bool {
Line 2,860:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 2,874:
=={{header|Tcl}}==
We can use <tt>[format %b]</tt> to format a number as binary, but ternary requires a custom proc:
<langsyntaxhighlight Tcllang="tcl">proc format_%t {n} {
while {$n} {
append r [expr {$n % 3}]
Line 2,881:
if {![info exists r]} {set r 0}
string reverse $r
}</langsyntaxhighlight>
 
Identifying palindromes is simple. This form is O(n) with a large constant factor, but good enough:
 
<langsyntaxhighlight Tcllang="tcl">proc pal? {s} {expr {$s eq [string reverse $s]}}</langsyntaxhighlight>
 
The naive approach turns out to be very slow:
<langsyntaxhighlight Tcllang="tcl">proc task {{find 6}} {
for {set i 0} {$find} {incr i} {
set b [format %b $i]
Line 2,899:
}
 
puts [time {task 4}]</langsyntaxhighlight>
{{out}}
<pre>Palindrome: 0 (0) (0)
Line 2,910:
We can do much better than that by naively iterating the binary palindromes. This is nice to do in a coroutine:
 
<langsyntaxhighlight Tcllang="tcl">package require Tcl 8.5 ;# for coroutines
 
proc 2pals {} {
Line 2,923:
yield ${a}1$b
}
}</langsyntaxhighlight>
 
The binary strings emitted by this generator are not in increasing order, but for this particular task, that turns out to be unimportant.
 
Our main loop needs only minor changes:
<langsyntaxhighlight Tcllang="tcl">proc task {{find 6}} {
coroutine gen apply {{} {yield; 2pals}}
while {$find} {
Line 2,942:
}
 
puts [time task]</langsyntaxhighlight>
This version finds the first 6 in under 4 seconds, which is good enough for the task at hand:
{{out}}
Line 2,956:
 
=={{header|VBA}}==
<langsyntaxhighlight lang="vb">Public Declare Function GetTickCount Lib "kernel32.dll" () As Long
'palindromes both in base3 and base2
'using Decimal data type to find number 6 and 7, although slowly
Line 3,089:
Debug.Print "Completed in"; (Time3 - Time1) / 1000; "seconds"
Application.ScreenUpdating = True
End Sub</langsyntaxhighlight>{{out}}<pre>' iter decimal binary ternary
' 0 0 0 0
' 0 1 1 1
Line 3,105:
{{libheader|Wren-fmt}}
Just the first 6 palindromes as the 7th is too large for Wren to process without resorting to BigInts.
<langsyntaxhighlight lang="ecmascript">import "/fmt" for Fmt
 
var isPalindrome2 = Fn.new { |n|
Line 3,178:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 3,218:
{{trans|Ruby}}
VERY slow after six but does find it.
<langsyntaxhighlight lang="zkl">fcn pal23W{ //--> iterator returning (index,palindromic number)
Walker.tweak(fcn(ri,r){ // references to loop start and count of palindromes
foreach i in ([ri.value..*]){
Line 3,230:
}
}.fp(Ref(3),Ref(3))).push(T(1,0),T(2,1)) // seed with first two results
}</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">foreach idx,n in (pal23W().walk(6)){
println("%2d: %,d == %.3B(3) == %.2B(2)".fmt(idx,n,n,n))
}</langsyntaxhighlight>
{{out}}
<pre>
10,333

edits

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