Quantcast
Viewing latest article 10
Browse Latest Browse All 23

Answer by MickyT for All about basic binary

SQL (PostgreSQL), 247.5255 230.25 (307 * .75)

Since SQL is known to be wonderful at these sorts of challenges, I thought I better put one together :) The bonus was really worthwhile for this one.
It should comply with spec, but I have no easy way to test the COPY I FROM STDIN.
Edit Fixed order. Changed the way column R is handled to use an array.

CREATE TABLE IF NOT EXISTS I(I INT);TRUNCATE TABLE I;COPY I FROM STDIN;WITH RECURSIVE R AS(SELECT n,I/n I,ARRAY[I%n] R FROM generate_series(2,(SELECT I FROM I))g(n),(SELECT I FROM I)I(I)UNION ALL SELECT n,I/n,I%n||R FROM R WHERE I>0)SELECT n||''||array_to_string(R,'')FROM R WHERE 2>ALL(R)and i=0ORDER BY n

As a test I just used straight inserts into the I table.Test run expanded and commented.

-- Create the table to accept the input from the copy commandCREATE TABLE IF NOT EXISTS I(I INT);-- Make sure that it is emptyTRUNCATE TABLE I;-- Popoulate it with a value from STDIN--COPY I FROM STDIN;INSERT INTO I VALUES(82000); -- Testing--Using a recursive CTE queryWITH RECURSIVE R AS (    -- Recursive anchor    SELECT n,                -- base for the row       I/n I,                -- integer division       ARRAY[I%n] R   -- put mod value in an array    FROM generate_series(2,(SELECT I FROM I))g(n), -- series for the bases         (SELECT I FROM I)I(I) -- Cross joined with I,  saves a few characters    UNION ALL     -- for each row from r recursively repeat the division and mod until i is 0    SELECT n,        I/n,        I%n||R -- Append mod to beginning of the array    FROM R WHERE I>0    )-- return from r where i=0 and r has 1's and 0's onlySELECT n||''||array_to_string(R,'')FROM R WHERE 2 > ALL(R)and i=0ORDER BY n -- Ensure correct order

2 10100000001010000
3 11011111001
4 110001100
5 10111000
81999 11
82000 10


Viewing latest article 10
Browse Latest Browse All 23

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>