Minimum positive multiple in base 10 using only 0 and 1: Difference between revisions
Minimum positive multiple in base 10 using only 0 and 1 (view source)
Revision as of 10:07, 4 January 2024
, 4 months ago→{{header|Wren}}: Minor tidy
m (→{{header|Phix}}: clarified inner test) |
m (→{{header|Wren}}: Minor tidy) |
||
(47 intermediate revisions by 20 users not shown) | |||
Line 1:
{{
Every positive integer has
This is simple to do, but can be challenging to do efficiently.
Line 41:
:* [[oeis:A004290|OEIS:A004290 Least positive multiple of n that when written in base 10 uses only 0's and 1's.]]
:* [https://math.stackexchange.com/questions/388165/how-to-find-the-smallest-number-with-just-0-and-1-which-is-divided-by-a-give How to find Minimum Positive Multiple in base 10 using only 0 and 1]
=={{header|11l}}==
{{trans|Kotlin}}
<syntaxhighlight lang="11l">F modp(m, n)
V result = m % n
I result < 0
result += n
R result
F getA004290(n)
I n == 1
R BigInt(1)
V arr = [[0] * n] * n
arr[0][0] = 1
arr[0][1] = 1
V m = 0
V ten = BigInt(10)
V nBi = BigInt(n)
L
m++
I arr[m - 1][Int(modp(-pow(ten, m), nBi))] == 1
L.break
arr[m][0] = 1
L(k) 1 .< n
arr[m][k] = max(arr[m - 1][k], arr[m - 1][Int(modp(BigInt(k) - pow(ten, m), nBi))])
V r = pow(ten, m)
V k = Int(modp(-r, nBi))
L(j) (m - 1 .< 0).step(-1)
I arr[j - 1][k] == 0
r += pow(ten, j)
k = Int(modp(BigInt(k) - pow(ten, j), nBi))
I k == 1
r++
R r
L(n) Array(1..10) [+] Array(95..105) [+] [297, 576, 594, 891, 909, 999, 1998, 2079, 2251, 2277, 2439, 2997, 4878]
V result = getA004290(n)
print(‘A004290(’n‘) = ’result‘ = ’n‘ * ’(result I/ n))</syntaxhighlight>
{{out}}
<pre>
A004290(1) = 1 = 1 * 1
A004290(2) = 10 = 2 * 5
A004290(3) = 111 = 3 * 37
A004290(4) = 100 = 4 * 25
A004290(5) = 10 = 5 * 2
A004290(6) = 1110 = 6 * 185
A004290(7) = 1001 = 7 * 143
A004290(8) = 1000 = 8 * 125
A004290(9) = 111111111 = 9 * 12345679
A004290(10) = 10 = 10 * 1
A004290(95) = 110010 = 95 * 1158
A004290(96) = 11100000 = 96 * 115625
A004290(97) = 11100001 = 97 * 114433
A004290(98) = 11000010 = 98 * 112245
A004290(99) = 111111111111111111 = 99 * 1122334455667789
A004290(100) = 100 = 100 * 1
A004290(101) = 101 = 101 * 1
A004290(102) = 1000110 = 102 * 9805
A004290(103) = 11100001 = 103 * 107767
A004290(104) = 1001000 = 104 * 9625
A004290(105) = 101010 = 105 * 962
A004290(297) = 1111011111111111111 = 297 * 3740778151889263
A004290(576) = 111111111000000 = 576 * 192901234375
A004290(594) = 11110111111111111110 = 594 * 18703890759446315
A004290(891) = 1111111111111111011 = 891 * 1247038284075321
A004290(909) = 1011111111111111111 = 909 * 1112333455567779
A004290(999) = 111111111111111111111111111 = 999 * 111222333444555666777889
A004290(1998) = 1111111111111111111111111110 = 1998 * 556111667222778333889445
A004290(2079) = 1001101101111111111111 = 2079 * 481530111164555609
A004290(2251) = 101101101111 = 2251 * 44913861
A004290(2277) = 11110111111111111011 = 2277 * 4879275850290343
A004290(2439) = 10000101011110111101111111 = 2439 * 4100082415379299344449
A004290(2997) = 1111110111111111111111111111 = 2997 * 370740777814851888925963
A004290(4878) = 100001010111101111011111110 = 4878 * 20500412076896496722245
</pre>
=={{header|ALGOL 68}}==
Based on a translation of Rick Heylen's C in the 'Binary' Puzzle link on the OIES site.
<syntaxhighlight lang="algol68">BEGIN
# returns B10 for the specified n or -1 if it cannot be found #
# based on Rick Heylen's C program linked from the OEIS site #
PROC find b10 = ( INT n )LONG INT:
IF n < 1 THEN -1
ELIF n = 1 THEN 1
ELSE
[ 0 : n ]LONG INT pow, val;
INT x, ten;
LONG INT count;
INT num = n;
FOR i FROM 0 TO n - 1 DO pow[ i ] := val[ i ] := 0 OD;
count := 0;
ten := 1;
x := 1;
WHILE x < n
AND BEGIN val[ x ] := ten;
FOR j FROM 0 TO n- 1 DO
IF pow[ j ] /= 0 THEN
IF INT j plus ten mod num = ( j + ten ) MOD num;
pow[ j plus ten mod num ] = 0
THEN
IF pow[ j ] /= x THEN pow[ j plus ten mod num ] := x FI
FI
FI
OD;
IF pow[ ten ] = 0 THEN pow[ ten ] := x FI;
ten *:= 10 MODAB num;
pow[ 0 ] = 0
END
DO
x +:= 1
OD;
x := num;
IF pow[ 0 ] = 0 THEN - 1 # couldn't find B10 #
ELSE
LONG INT result := 0;
WHILE x /= 0 DO
WHILE ( count -:= 1 ) > pow[ x MOD num ] - 1 DO result *:= 10 OD;
count := pow [ x MOD num ] -1;
result *:= 10 +:= 1;
x := SHORTEN ( num + x - val[ SHORTEN pow[ x MOD num ] ] ) MOD num
OD;
WHILE count > 0 DO result *:= 10 ; count -:= 1 OD;
result
FI
FI # find B10 # ;
# outputs B10 for the specified n #
PROC show b10 = ( INT n )VOID:
IF LONG INT b10 = find b10( n );
b10 < 1
THEN print( ( "Cannot find B10 for: ", whole( n, 0 ), newline ) )
ELSE
# found B10(n) #
print( ( whole( n, -6 ), ": ", whole( b10, -32 ), " = ", whole( n, -6 ), " * ", whole( b10 OVER n, 0 ), newline ) )
FI;
# task test cases #
FOR n FROM 1 TO 10 DO show b10( n ) OD;
FOR n FROM 95 TO 105 DO show b10( n ) OD;
[]INT tests = ( 297, 576, 594, 891, 909, 999
, 1998, 2079, 2251, 2277
, 2439, 2997, 4878
);
FOR n FROM LWB tests TO UPB tests DO show b10( tests[ n ] ) OD
END
</syntaxhighlight>
{{out}}
<pre>
1: 1 = 1 * 1
2: 10 = 2 * 5
3: 111 = 3 * 37
4: 100 = 4 * 25
5: 10 = 5 * 2
6: 1110 = 6 * 185
7: 1001 = 7 * 143
8: 1000 = 8 * 125
9: 111111111 = 9 * 12345679
10: 10 = 10 * 1
95: 110010 = 95 * 1158
96: 11100000 = 96 * 115625
97: 11100001 = 97 * 114433
98: 11000010 = 98 * 112245
99: 111111111111111111 = 99 * 1122334455667789
100: 100 = 100 * 1
101: 101 = 101 * 1
102: 1000110 = 102 * 9805
103: 11100001 = 103 * 107767
104: 1001000 = 104 * 9625
105: 101010 = 105 * 962
297: 1111011111111111111 = 297 * 3740778151889263
576: 111111111000000 = 576 * 192901234375
594: 11110111111111111110 = 594 * 18703890759446315
891: 1111111111111111011 = 891 * 1247038284075321
909: 1011111111111111111 = 909 * 1112333455567779
999: 111111111111111111111111111 = 999 * 111222333444555666777889
1998: 1111111111111111111111111110 = 1998 * 556111667222778333889445
2079: 1001101101111111111111 = 2079 * 481530111164555609
2251: 101101101111 = 2251 * 44913861
2277: 11110111111111111011 = 2277 * 4879275850290343
2439: 10000101011110111101111111 = 2439 * 4100082415379299344449
2997: 1111110111111111111111111111 = 2997 * 370740777814851888925963
4878: 100001010111101111011111110 = 4878 * 20500412076896496722245
</pre>
=={{header|AppleScript}}==
{{trans|Phix}}
… reworked to be more understandable to myself. It returns the required results in 0.476 seconds on my machine, beating my own abandoned effort's 42 minutes and 16 seconds by some margin! I've used my own script's long-division code to get the multiplier and have incorporated the optimisation used at the top of the handler.
<syntaxhighlight lang="applescript">on b10(n)
if (n < 1) then error
-- Each factor of n that's either 10, 2, or 5 will contribute just a zero each to the end of the B10.
-- Count and lose any such factors here to leave a potentially much smaller n (nx) to process.
-- The relevant number of zeros will be appended to the shorter result afterwards.
set nx to n
set endZeroCount to 0
repeat with factor in {10, 5, 2}
repeat while (nx mod factor = 0)
set endZeroCount to endZeroCount + 1
set nx to nx div factor
end repeat
end repeat
script o
-- 'val' and 'pow' are uninformative and misleading labels for these lists, but I've
-- kept them for code comparison purposes and because I can't think of anything better.
property val : {} -- Will be successive powers of 10, mod nx.
property pow : {} -- Initially nx placeholders, some or all of which will be replaced with indices to slots in val.
property digits : {} -- Digits for output.
end script
if (nx = 1) then
set end of o's digits to 1 -- Clever stuff not needed.
else
set placeholder to missing value
repeat nx times
set end of o's pow to placeholder
end repeat
-- Calculate successive powers of 10, mod nx, (hereafter "powers"), storing them in val and
-- finding all the sums-mod-nx ("sums") that can be made from them and sums already obtained.
-- The range of possible sums (0 to nx - 1) is represented by positions in pow (index = sum + 1
-- with AppleScript's 1-based indices). Assign the index in val of the first power to produce
-- each sum to pow's slot for that sum. Stop when the index of the current power turns up in
-- pow's first slot, indicating that a subset of the powers obtained sums to nx (sum mod nx = 0)
-- and thus that the sum of the corresponding /un/modded powers of 10 is a multiple of nx.
-- The first power of 10 is always 1, its index in val is always 1, and its only possible sum is itself.
set p10 to 1 -- 10 ^ 0 mod nx.
set end of o's val to p10
set valIndex to 1
set item (p10 + 1) of o's pow to valIndex
-- Subsequent settings depend on the value of nx.
repeat until (beginning of o's pow = valIndex)
-- Get the next power of 10, mod nx.
set p10 to p10 * 10 mod nx
set end of o's val to p10
set valIndex to valIndex + 1
-- "Add" it to any existing sums by inserting its val index into the pow slot p10 places after (mod nx)
-- each slot already set for a lower-order p10, provided the target slot itself isn't already set.
repeat with powIndex from 1 to nx
set this to item powIndex of o's pow
if (this is placeholder) then
else if (this < valIndex) then
set targetIndex to (powIndex + p10 - 1) mod nx + 1
if (item targetIndex of o's pow is placeholder) then set item targetIndex of o's pow to valIndex
end if
end repeat
-- Ensure that it's also treated as a sum in its own right.
set targetIndex to p10 + 1
if (item targetIndex of o's pow is placeholder) then set item targetIndex of o's pow to valIndex
end repeat
-- To identify the powers-mod-nx which summed to nx, use pow unnecessarily to look up the index
-- of the power still in p10 which allowed the sum, subtract that power from the sum, use pow again
-- to find the lower-order power that allowed what's left, and so on until the sum's reduced to 0.
-- Append a 1 to the digit list for each power identified and a 0 each for any intervening ones.
set sum to nx
set previousIndex to 0
repeat until (sum = 0)
set valIndex to (item (sum mod nx + 1) of o's pow)
repeat (previousIndex - valIndex - 1) times
set end of o's digits to 0
end repeat
set end of o's digits to 1
set previousIndex to valIndex
set sum to (sum - (item valIndex of o's val) + nx) mod nx
end repeat
end if
-- Append any trailing zeros due from factors removed at the beginning.
repeat endZeroCount times
set end of o's digits to 0
end repeat
-- Coerce the list of 1s and 0s to text.
set bTen to join(o's digits, "")
-- Replace the list's contents with the results of dividing by the original n.
set digitCount to (count o's digits)
set remainder to 0
repeat with i from 1 to digitCount
set dividend to remainder * 10 + (item i of o's digits)
set item i of o's digits to dividend div n
set remainder to dividend mod n
end repeat
-- Zap any leading zeros in the result and coerce what's left to text.
repeat with i from 1 to digitCount
if (item i of o's digits > 0) then exit repeat
set item i of o's digits to ""
end repeat
set multiplier to join(o's digits, "")
return {n:n, b10:bTen, multiplier:multiplier}
end b10
on join(lst, delim)
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to delim
set txt to lst as text
set AppleScript's text item delimiters to astid
return txt
end join
local output, n, bTen, multiplier
set output to {}
repeat with n in {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, ¬
297, 576, 594, 891, 909, 999, 1998, 2079, 2251, 2277, 2439, 2997, 4878}
set {b10:bTen, multiplier:multiplier} to b10(n's contents)
set end of output to (n as text) & " * " & multiplier & (" = " & bTen)
end repeat
return join(output, linefeed)</syntaxhighlight>
{{out}}
<syntaxhighlight lang="applescript">"1 * 1 = 1
2 * 5 = 10
3 * 37 = 111
4 * 25 = 100
5 * 2 = 10
6 * 185 = 1110
7 * 143 = 1001
8 * 125 = 1000
9 * 12345679 = 111111111
10 * 1 = 10
95 * 1158 = 110010
96 * 115625 = 11100000
97 * 114433 = 11100001
98 * 112245 = 11000010
99 * 1122334455667789 = 111111111111111111
100 * 1 = 100
101 * 1 = 101
102 * 9805 = 1000110
103 * 107767 = 11100001
104 * 9625 = 1001000
105 * 962 = 101010
297 * 3740778151889263 = 1111011111111111111
576 * 192901234375 = 111111111000000
594 * 18703890759446315 = 11110111111111111110
891 * 1247038284075321 = 1111111111111111011
909 * 1112333455567779 = 1011111111111111111
999 * 111222333444555666777889 = 111111111111111111111111111
1998 * 556111667222778333889445 = 1111111111111111111111111110
2079 * 481530111164555609 = 1001101101111111111111
2251 * 44913861 = 101101101111
2277 * 4879275850290343 = 11110111111111111011
2439 * 4100082415379299344449 = 10000101011110111101111111
2997 * 370740777814851888925963 = 1111110111111111111111111111
4878 * 20500412076896496722245 = 100001010111101111011111110"</syntaxhighlight>
=={{header|C}}==
Compiled using gcc for the 128 bit integer type
<syntaxhighlight lang="c">#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
__int128 imax(__int128 a, __int128 b) {
if (a > b) {
return a;
}
return b;
}
__int128 ipow(__int128 b, __int128 n) {
__int128 res;
if (n == 0) {
return 1;
}
if (n == 1) {
return b;
}
res = b;
while (n > 1) {
res *= b;
n--;
}
return res;
}
__int128 imod(__int128 m, __int128 n) {
__int128 result = m % n;
if (result < 0) {
result += n;
}
return result;
}
bool valid(__int128 n) {
if (n < 0) {
return false;
}
while (n > 0) {
int r = n % 10;
if (r > 1) {
return false;
}
n /= 10;
}
return true;
}
__int128 mpm(const __int128 n) {
__int128 *L;
__int128 m, k, r, j;
if (n == 1) {
return 1;
}
L = calloc(n * n, sizeof(__int128));
L[0] = 1;
L[1] = 1;
m = 0;
while (true) {
m++;
if (L[(m - 1) * n + imod(-ipow(10, m), n)] == 1) {
break;
}
L[m * n + 0] = 1;
for (k = 1; k < n; k++) {
L[m * n + k] = imax(L[(m - 1) * n + k], L[(m - 1) * n + imod(k - ipow(10, m), n)]);
}
}
r = ipow(10, m);
k = imod(-r, n);
for (j = m - 1; j >= 1; j--) {
if (L[(j - 1) * n + k] == 0) {
r = r + ipow(10, j);
k = imod(k - ipow(10, j), n);
}
}
if (k == 1) {
r++;
}
return r / n;
}
void print128(__int128 n) {
char buffer[64]; // more then is needed, but is nice and round;
int pos = (sizeof(buffer) / sizeof(char)) - 1;
bool negative = false;
if (n < 0) {
negative = true;
n = -n;
}
buffer[pos] = 0;
while (n > 0) {
int rem = n % 10;
buffer[--pos] = rem + '0';
n /= 10;
}
if (negative) {
buffer[--pos] = '-';
}
printf(&buffer[pos]);
}
void test(__int128 n) {
__int128 mult = mpm(n);
if (mult > 0) {
print128(n);
printf(" * ");
print128(mult);
printf(" = ");
print128(n * mult);
printf("\n");
} else {
print128(n);
printf("(no solution)\n");
}
}
int main() {
int i;
// 1-10 (inclusive)
for (i = 1; i <= 10; i++) {
test(i);
}
// 95-105 (inclusive)
for (i = 95; i <= 105; i++) {
test(i);
}
test(297);
test(576);
test(594); // needs a larger number type (64 bits signed)
test(891);
test(909);
test(999); // needs a larger number type (87 bits signed)
// optional
test(1998);
test(2079);
test(2251);
test(2277);
// stretch
test(2439);
test(2997);
test(4878);
return 0;
}</syntaxhighlight>
{{out}}
<pre>1 * 1 = 1
2 * 5 = 10
3 * 37 = 111
4 * 25 = 100
5 * 2 = 10
6 * 185 = 1110
7 * 143 = 1001
8 * 125 = 1000
9 * 12345679 = 111111111
10 * 1 = 10
95 * 1158 = 110010
96 * 115625 = 11100000
97 * 114433 = 11100001
98 * 112245 = 11000010
99 * 1122334455667789 = 111111111111111111
100 * 1 = 100
101 * 1 = 101
102 * 9805 = 1000110
103 * 107767 = 11100001
104 * 9625 = 1001000
105 * 962 = 101010
297 * 3740778151889263 = 1111011111111111111
576 * 192901234375 = 111111111000000
594 * 18703890759446315 = 11110111111111111110
891 * 1247038284075321 = 1111111111111111011
909 * 1112333455567779 = 1011111111111111111
999 * 111222333444555666777889 = 111111111111111111111111111
1998 * 556111667222778333889445 = 1111111111111111111111111110
2079 * 481530111164555609 = 1001101101111111111111
2251 * 44913861 = 101101101111
2277 * 4879275850290343 = 11110111111111111011
2439 * 4100082415379299344449 = 10000101011110111101111111
2997 * 370740777814851888925963 = 1111110111111111111111111111
4878 * 20500412076896496722245 = 100001010111101111011111110</pre>
=={{header|C#|Csharp}}==
Translated from the C code for the 'Binary' Puzzle algorithm, from the OEIS link. Note that since this task asks for a maximum of 28 digits for "B10", that only a 32 bit integer is required, even though the original code uses 64 bit integers. Not a huge point, but performance is noticeably improved.
Since C# has a ''decimal'' type, it can be used instead of an external 128 bit integer package to compute the multiplier.
<syntaxhighlight lang="csharp">using System;
using System.Collections.Generic;
using static System.Console;
class Program {
static string B10(int n) {
int[] pow = new int[n + 1], val = new int[29];
for (int count = 0, ten = 1, x = 1; x <= n; x++) {
val[x] = ten;
for (int j = 0, t; j <= n; j++)
if (pow[j] != 0 && pow[j] != x && pow[t = (j + ten) % n] == 0)
pow[t] = x;
if (pow[ten] == 0) pow[ten] = x;
ten = (10 * ten) % n;
if (pow[0] != 0) {
x = n;
string s = "";
while (x != 0) {
int p = pow[x % n];
if (count > p) s += new string('0', count - p);
count = p - 1;
s += "1";
x = (n + x - val[p]) % n;
}
if (count > 0) s += new string('0', count);
return s;
}
}
return "1";
}
static void Main(string[] args) {
string fmt = "{0,4} * {1,24} = {2,-28}\n";
int[] m = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105,
297, 576, 594, 891, 909, 999, 1998, 2079, 2251, 2277, 2439, 2997, 4878 };
string[] r = new string[m.Length];
WriteLine(fmt + new string('-', 62), "n", "multiplier", "B10");
var sw = System.Diagnostics.Stopwatch.StartNew();
for (int i = 0; i < m.Length; i++) r[i] = B10(m[i]);
sw.Stop();
for (int i = 0; i < m.Length; i++) Write(fmt, m[i], decimal.Parse(r[i]) / m[i], r[i]);
Write("\nTook {0}ms", sw.Elapsed.TotalMilliseconds);
}
}</syntaxhighlight>
{{out}}
<pre> n * multiplier = B10
--------------------------------------------------------------
1 * 1 = 1
2 * 5 = 10
3 * 37 = 111
4 * 25 = 100
5 * 2 = 10
6 * 185 = 1110
7 * 143 = 1001
8 * 125 = 1000
9 * 12345679 = 111111111
10 * 1 = 10
95 * 1158 = 110010
96 * 115625 = 11100000
97 * 114433 = 11100001
98 * 112245 = 11000010
99 * 1122334455667789 = 111111111111111111
100 * 1 = 100
101 * 1 = 101
102 * 9805 = 1000110
103 * 107767 = 11100001
104 * 9625 = 1001000
105 * 962 = 101010
297 * 3740778151889263 = 1111011111111111111
576 * 192901234375 = 111111111000000
594 * 18703890759446315 = 11110111111111111110
891 * 1247038284075321 = 1111111111111111011
909 * 1112333455567779 = 1011111111111111111
999 * 111222333444555666777889 = 111111111111111111111111111
1998 * 556111667222778333889445 = 1111111111111111111111111110
2079 * 481530111164555609 = 1001101101111111111111
2251 * 44913861 = 101101101111
2277 * 4879275850290343 = 11110111111111111011
2439 * 4100082415379299344449 = 10000101011110111101111111
2997 * 370740777814851888925963 = 1111110111111111111111111111
4878 * 20500412076896496722245 = 100001010111101111011111110
Took 1.9585ms
</pre>Timing from tio.run
=={{header|C++}}==
{{trans|C}}
<syntaxhighlight lang="cpp">#include <iostream>
#include <vector>
__int128 imax(__int128 a, __int128 b) {
if (a > b) {
return a;
}
return b;
}
__int128 ipow(__int128 b, __int128 n) {
if (n == 0) {
return 1;
}
if (n == 1) {
return b;
}
__int128 res = b;
while (n > 1) {
res *= b;
n--;
}
return res;
}
__int128 imod(__int128 m, __int128 n) {
__int128 result = m % n;
if (result < 0) {
result += n;
}
return result;
}
bool valid(__int128 n) {
if (n < 0) {
return false;
}
while (n > 0) {
int r = n % 10;
if (r > 1) {
return false;
}
n /= 10;
}
return true;
}
__int128 mpm(const __int128 n) {
if (n == 1) {
return 1;
}
std::vector<__int128> L(n * n, 0);
L[0] = 1;
L[1] = 1;
__int128 m, k, r, j;
m = 0;
while (true) {
m++;
if (L[(m - 1) * n + imod(-ipow(10, m), n)] == 1) {
break;
}
L[m * n + 0] = 1;
for (k = 1; k < n; k++) {
L[m * n + k] = imax(L[(m - 1) * n + k], L[(m - 1) * n + imod(k - ipow(10, m), n)]);
}
}
r = ipow(10, m);
k = imod(-r, n);
for (j = m - 1; j >= 1; j--) {
if (L[(j - 1) * n + k] == 0) {
r = r + ipow(10, j);
k = imod(k - ipow(10, j), n);
}
}
if (k == 1) {
r++;
}
return r / n;
}
std::ostream& operator<<(std::ostream& os, __int128 n) {
char buffer[64]; // more then is needed, but is nice and round;
int pos = (sizeof(buffer) / sizeof(char)) - 1;
bool negative = false;
if (n < 0) {
negative = true;
n = -n;
}
buffer[pos] = 0;
while (n > 0) {
int rem = n % 10;
buffer[--pos] = rem + '0';
n /= 10;
}
if (negative) {
buffer[--pos] = '-';
}
return os << &buffer[pos];
}
void test(__int128 n) {
__int128 mult = mpm(n);
if (mult > 0) {
std::cout << n << " * " << mult << " = " << (n * mult) << '\n';
} else {
std::cout << n << "(no solution)\n";
}
}
int main() {
int i;
// 1-10 (inclusive)
for (i = 1; i <= 10; i++) {
test(i);
}
// 95-105 (inclusive)
for (i = 95; i <= 105; i++) {
test(i);
}
test(297);
test(576);
test(594); // needs a larger number type (64 bits signed)
test(891);
test(909);
test(999); // needs a larger number type (87 bits signed)
// optional
test(1998);
test(2079);
test(2251);
test(2277);
// stretch
test(2439);
test(2997);
test(4878);
return 0;
}</syntaxhighlight>
{{out}}
<pre>1 * 1 = 1
2 * 5 = 10
3 * 37 = 111
4 * 25 = 100
5 * 2 = 10
6 * 185 = 1110
7 * 143 = 1001
8 * 125 = 1000
9 * 12345679 = 111111111
10 * 1 = 10
95 * 1158 = 110010
96 * 115625 = 11100000
97 * 114433 = 11100001
98 * 112245 = 11000010
99 * 1122334455667789 = 111111111111111111
100 * 1 = 100
101 * 1 = 101
102 * 9805 = 1000110
103 * 107767 = 11100001
104 * 9625 = 1001000
105 * 962 = 101010
297 * 3740778151889263 = 1111011111111111111
576 * 192901234375 = 111111111000000
594 * 18703890759446315 = 11110111111111111110
891 * 1247038284075321 = 1111111111111111011
909 * 1112333455567779 = 1011111111111111111
999 * 111222333444555666777889 = 111111111111111111111111111
1998 * 556111667222778333889445 = 1111111111111111111111111110
2079 * 481530111164555609 = 1001101101111111111111
2251 * 44913861 = 101101101111
2277 * 4879275850290343 = 11110111111111111011
2439 * 4100082415379299344449 = 10000101011110111101111111
2997 * 370740777814851888925963 = 1111110111111111111111111111
4878 * 20500412076896496722245 = 100001010111101111011111110</pre>
=={{header|D}}==
{{trans|Java}}
<syntaxhighlight lang="d">import std.algorithm;
import std.array;
import std.bigint;
import std.range;
import std.stdio;
void main() {
foreach (n; chain(iota(1, 11), iota(95, 106), only(297, 576, 594, 891, 909, 999, 1998, 2079, 2251, 2277, 2439, 2997, 4878))) {
auto result = getA004290(n);
writefln("A004290(%d) = %s = %s * %s", n, result, n, result / n);
}
}
BigInt getA004290(int n) {
if (n == 1) {
return BigInt(1);
}
auto L = uninitializedArray!(int[][])(n, n);
foreach (i; 2..n) {
L[0][i] = 0;
}
L[0][0] = 1;
L[0][1] = 1;
int m = 0;
BigInt ten = 10;
BigInt nBi = n;
while (true) {
m++;
if (L[m - 1][mod(-(ten ^^ m), nBi).toInt] == 1) {
break;
}
L[m][0] = 1;
foreach (k; 1..n) {
L[m][k] = max(L[m - 1][k], L[m - 1][mod(BigInt(k) - (ten ^^ m), nBi).toInt]);
}
}
auto r = ten ^^ m;
auto k = mod(-r, nBi);
foreach_reverse (j; 1 .. m) {
assert(j != m);
if (L[j - 1][k.toInt] == 0) {
r += ten ^^ j;
k = mod(k - (ten ^^ j), nBi);
}
}
if (k == 1) {
r++;
}
return r;
}
BigInt mod(BigInt m, BigInt n) {
auto result = m % n;
if (result < 0) {
result += n;
}
return result;
}</syntaxhighlight>
{{out}}
<pre>A004290(1) = 1 = 1 * 1
A004290(2) = 10 = 2 * 5
A004290(3) = 111 = 3 * 37
A004290(4) = 100 = 4 * 25
A004290(5) = 10 = 5 * 2
A004290(6) = 1110 = 6 * 185
A004290(7) = 1001 = 7 * 143
A004290(8) = 1000 = 8 * 125
A004290(9) = 111111111 = 9 * 12345679
A004290(10) = 10 = 10 * 1
A004290(95) = 110010 = 95 * 1158
A004290(96) = 11100000 = 96 * 115625
A004290(97) = 11100001 = 97 * 114433
A004290(98) = 11000010 = 98 * 112245
A004290(99) = 111111111111111111 = 99 * 1122334455667789
A004290(100) = 100 = 100 * 1
A004290(101) = 101 = 101 * 1
A004290(102) = 1000110 = 102 * 9805
A004290(103) = 11100001 = 103 * 107767
A004290(104) = 1001000 = 104 * 9625
A004290(105) = 101010 = 105 * 962
A004290(297) = 1111011111111111111 = 297 * 3740778151889263
A004290(576) = 111111111000000 = 576 * 192901234375
A004290(594) = 11110111111111111110 = 594 * 18703890759446315
A004290(891) = 1111111111111111011 = 891 * 1247038284075321
A004290(909) = 1011111111111111111 = 909 * 1112333455567779
A004290(999) = 111111111111111111111111111 = 999 * 111222333444555666777889
A004290(1998) = 1111111111111111111111111110 = 1998 * 556111667222778333889445
A004290(2079) = 1001101101111111111111 = 2079 * 481530111164555609
A004290(2251) = 101101101111 = 2251 * 44913861
A004290(2277) = 11110111111111111011 = 2277 * 4879275850290343
A004290(2439) = 10000101011110111101111111 = 2439 * 4100082415379299344449
A004290(2997) = 1111110111111111111111111111 = 2997 * 370740777814851888925963
A004290(4878) = 100001010111101111011111110 = 4878 * 20500412076896496722245
</pre>
=={{header|F_Sharp|F#}}==
<
// Minimum positive multiple in base 10 using only 0 and 1. Nigel Galloway: March 9th., 2020
let rec fN Σ n i g e l=if l=1||n=1 then Σ+1I else
Line 55 ⟶ 972:
List.concat[[1..10];[95..105];[297;576;594;891;909;999;1998;2079;2251;2277;2439;2997;4878]]
|>List.iter(fun n->let g=B10 n in printfn "%d * %A = %A" n (g/bigint(n)) g)
</syntaxhighlight>
{{out}}
<pre>
Line 94 ⟶ 1,011:
Real: 00:00:00.454, CPU: 00:00:00.640, GC gen0: 107, gen1: 3, gen2: 1
</pre>
=={{header|Factor}}==
This is just the naive implementation, converting the number to a string and checking every character.
<syntaxhighlight lang="factor">: is-1-or-0 ( char -- ? ) dup CHAR: 0 = [ drop t ] [ CHAR: 1 = ] if ;
: int-is-B10 ( n -- ? ) unparse [ is-1-or-0 ] all? ;
: B10-step ( x x -- x x ? ) dup int-is-B10 [ f ] [ over + t ] if ;
: find-B10 ( x -- x ) dup [ B10-step ] loop nip ;</syntaxhighlight>
=={{header|FreeBASIC}}==
{{trans|Ring}}
<syntaxhighlight lang="freebasic">#define limit1 900
#define limit2 18703890759446315
Dim As Ulongint nplus, lenplus, prod
Dim As Boolean plusflag = false, flag
Dim As String pstr
Dim As Integer plus(6) = {297,576,594,891,909,999}
Print !"Minimum positive multiple in base 10 using only 0 and 1:\n"
Print " N * multiplier = B10"
For n As Ulongint = 1 To limit1
If n = 106 Then
plusflag = true
nplus = 0
End If
lenplus = Ubound(plus)
If plusflag = true Then
nplus += 1
If nplus < lenplus+1 Then
n = plus(nplus)
Else
Exit For
End If
End If
For m As Ulongint = 1 To limit2
flag = true
prod = n*m
pstr = Str(prod)
For p As Ulongint = 1 To Len(pstr)
If Not(Mid(pstr,p,1) = "0" Or Mid(pstr,p,1) = "1") Then
flag = false
Exit For
End If
Next p
If flag = true Then
Print Using "### * ################ = &"; n; m; pstr
Exit For
End If
Next m
If n = 10 Then n = 94 : End If
Next n
Sleep</syntaxhighlight>
=={{header|Go}}==
Line 101 ⟶ 1,070:
To work out the multipliers for this task, we need to deal with numbers up to 28 digits long. As Go doesn't natively support uint128, I've used a third party library instead which appears to be much quicker than big.Int.
<
import (
Line 174 ⟶ 1,143:
}
fmt.Printf("\nTook %s\n", time.Since(start))
}</
{{out}}
Line 222 ⟶ 1,191:
A direct encoding, without any special optimizations, of the approach described in the Math.StackExchange article referenced in the task description.
<syntaxhighlight lang
import Data.List (find)
import Data.Maybe (isJust)
-- MINIMUM POSITIVE MULTIPLE IN BASE 10 USING ONLY 0 AND 1
b10 :: Integral a => a -> Integer
b10 n = read (digitMatch rems sums) :: Integer
where
Line 235 ⟶ 1,203:
until
(\(_, _, _, mb) -> isJust mb)
( \(e, rems, ms, _) ->
let m = rem (10 ^ e) n
newSums =
in ( succ e,
ms <> newSums,
find
( (0 ==) . flip rem n . fst
)
newSums
)
)
(0, [], [], Nothing)
digitMatch :: Eq a => [a] -> [a] -> String
digitMatch [] _ = []
digitMatch xs [] = '0' <$ xs
digitMatch (x : xs) yys@(y : ys)
| otherwise = '1' : digitMatch xs ys
--------------------------- TEST
main :: IO ()
main =
mapM_
(
. ( \x ->
let b = b10 x
in justifyRight 5 ' ' (show x)
<> justifyLeft 25 ' ' (show $ div b x)
<> " -> "
<> show b
)
)
( [1 .. 10]
<> [95 .. 105]
<> [297, 576, 594, 891, 909, 999]
)
justifyLeft, justifyRight :: Int -> a -> [a] -> [a]
justifyLeft n c s = take n (s <> replicate n c)
justifyRight n c = (drop . length) <*> (replicate n c <>)</syntaxhighlight>
{{Out}}
<pre> 1 * 1 -> 1
Line 292 ⟶ 1,276:
909 * 1112333455567779 -> 1011111111111111111
999 * 111222333444555666777889 -> 111111111111111111111111111</pre>
=={{header|J}}==
Implementation derived from the maple algorithm at https://oeis.org/A004290:
<syntaxhighlight lang="j">B10=: {{ NB. https://oeis.org/A004290
next=. {{ {: (u -) 10x^# }}
step=. ([>. [ {~ y|(i.y)+]) next
continue=. 0 = ({~y|]) next
L=.1 0,~^:(y>1) (, step)^:continue^:_ ,:y{.1 1
k=. y|-r=.10x^<:#L
for_j. i.-<:#L do.
if. 0=L{~<k,~j-1 do.
k=. y|k-E=. 10x^j
r=. r+E
end.
end. r assert. 0=y|r
}}</syntaxhighlight>
Task example:
<syntaxhighlight lang="j">through=: {{ m+i.1+n-m }}
(,.B10"0) 1 through 10, 95 through 105, 297, 576, 594, 891, 909, 999, 1998, 2079, 2251, 2277, 2439, 2997, 4878
1 1
2 10
3 111
4 100
5 10
6 1110
7 1001
8 1000
9 111111111
10 10
95 110010
96 11100000
97 11100001
98 11000010
99 111111111111111111
100 100
101 101
102 1000110
103 11100001
104 1001000
105 101010
297 1111011111111111111
576 111111111000000
594 11110111111111111110
891 1111111111111111011
909 1011111111111111111
999 111111111111111111111111111
1998 1111111111111111111111111110
2079 1001101101111111111111
2251 101101101111
2277 11110111111111111011
2439 10000101011110111101111111
2997 1111110111111111111111111111
4878 100001010111101111011111110</syntaxhighlight>
=={{header|Java}}==
Implementation of algorithm from the OIES site.
<
import java.math.BigInteger;
import java.util.ArrayList;
Line 391 ⟶ 1,433:
}
}
</syntaxhighlight>
{{Out}}
<pre>
Line 428 ⟶ 1,470:
A004290(2997) = 1111110111111111111111111111 = 2997 * 370740777814851888925963
A004290(4878) = 100001010111101111011111110 = 4878 * 20500412076896496722245
</pre>
=={{header|jq}}==
{{trans|Wren}}
'''Works with gojq, the Go implementation of jq'''
The C implementation of jq does not have sufficiently accurate integer arithmetic
to get beyond n = 98, so only the results for a run of gojq are shown.
<syntaxhighlight lang="jq">def lpad($len): tostring | ($len - length) as $l | (" " * $l)[:$l] + .;
def pp(a;b;c): "\(a|lpad(4)): \(b|lpad(28)) \(c|lpad(24))";
def b10:
. as $n
| if . == 1 then pp(1;1;1)
else ($n + 1) as $n1
| { pow: [range(0;$n)|0],
val: [range(0;$n)|0],
count: 0,
ten: 1,
x: 1 }
| until (.x >= $n1;
.val[.x] = .ten
| reduce range(0;$n1) as $j (.;
if .pow[$j] != 0 and .pow[($j+.ten)%$n] == 0 and .pow[$j] != .x
then .pow[($j+.ten)%$n] = .x
else . end )
| if .pow[.ten] == 0 then .pow[.ten] = .x else . end
| .ten = (10*.ten) % $n
| if .pow[0] != 0 then .x = $n1 # .x will soon be reset
else .x += 1
end )
| .x = $n
| if .pow[0] != 0
then .s = ""
| until (.x == 0;
.pow[.x % $n] as $p
| if .count > $p then .s += ("0" * (.count-$p)) else . end
| .count = $p - 1
| .s += "1"
| .x = ( ($n + .x - .val[$p]) % $n ) )
| if .count > 0 then .s += ("0" * .count) else . end
| pp($n; .s; .s|tonumber/$n)
else "Can't do it!"
end
end;
def tests: [
[1, 10], [95, 105], [297], [576], [594], [891], [909], [999],
[1998], [2079], [2251], [2277], [2439], [2997], [4878]
];
pp("n"; "B10"; "multiplier"),
(pp("-";"-";"-") | gsub(".";"-")),
( tests[]
| .[0] as $from
| (if length == 2 then .[1] else $from end) as $to
| range($from; $to + 1)
| b10 )</syntaxhighlight>
{{out}}
<pre>
n: B10 multiplier
-----------------------------------------------------------
1: 1 1
2: 10 5
3: 111 37
4: 100 25
5: 10 2
6: 1110 185
7: 1001 143
8: 1000 125
9: 111111111 12345679
10: 10 1
95: 110010 1158
96: 11100000 115625
97: 11100001 114433
98: 11000010 112245
99: 111111111111111111 1122334455667789
100: 100 1
101: 101 1
102: 1000110 9805
103: 11100001 107767
104: 1001000 9625
105: 101010 962
297: 1111011111111111111 3740778151889263
576: 111111111000000 192901234375
594: 11110111111111111110 18703890759446315
891: 1111111111111111011 1247038284075321
909: 1011111111111111111 1112333455567779
999: 111111111111111111111111111 111222333444555666777889
1998: 1111111111111111111111111110 556111667222778333889445
2079: 1001101101111111111111 481530111164555609
2251: 101101101111 44913861
2277: 11110111111111111011 4879275850290343
2439: 10000101011110111101111111 4100082415379299344449
2997: 1111110111111111111111111111 370740777814851888925963
4878: 100001010111101111011111110 20500412076896496722245
</pre>
=={{header|Julia}}==
Uses the iterate in base 2, check for a multiple in base 10 method. Still slow but gets time below 1 minute by calculating the base 10 number within the same loop as the one that finds the base 2 digits.
<
for i in Int128(1):typemax(Int128)
q, b10, place = i, zero(Int128), one(Int128)
Line 452 ⟶ 1,592:
println("B10($n) = $n * $(div(i, n)) = $i")
end
</
<pre>
B10(1) = 1 * 1 = 1
Line 490 ⟶ 1,630:
=== Puzzle algorithm version ===
{{trans|Phix}}
<
n == 1 && return one(Int128)
num, count, ten = n + 1, 0, 1
Line 530 ⟶ 1,670:
println("B10($n) = $n * $(div(i, n)) = $i")
end
</
<pre>
B10(1) = 1 * 1 = 1
Line 569 ⟶ 1,709:
0.054239 seconds (1.67 k allocations: 917.734 KiB)
</pre>
=={{header|Kotlin}}==
{{trans|Java}}
<syntaxhighlight lang="scala">import java.math.BigInteger
fun main() {
for (n in testCases) {
val result = getA004290(n)
println("A004290($n) = $result = $n * ${result / n.toBigInteger()}")
}
}
private val testCases: List<Int>
get() {
val testCases: MutableList<Int> = ArrayList()
for (i in 1..10) {
testCases.add(i)
}
for (i in 95..105) {
testCases.add(i)
}
for (i in intArrayOf(297, 576, 594, 891, 909, 999, 1998, 2079, 2251, 2277, 2439, 2997, 4878)) {
testCases.add(i)
}
return testCases
}
private fun getA004290(n: Int): BigInteger {
if (n == 1) {
return BigInteger.ONE
}
val arr = Array(n) { IntArray(n) }
for (i in 2 until n) {
arr[0][i] = 0
}
arr[0][0] = 1
arr[0][1] = 1
var m = 0
val ten = BigInteger.TEN
val nBi = n.toBigInteger()
while (true) {
m++
if (arr[m - 1][mod(-ten.pow(m), nBi).toInt()] == 1) {
break
}
arr[m][0] = 1
for (k in 1 until n) {
arr[m][k] = arr[m - 1][k].coerceAtLeast(arr[m - 1][mod(k.toBigInteger() - ten.pow(m), nBi).toInt()])
}
}
var r = ten.pow(m)
var k = mod(-r, nBi)
for (j in m - 1 downTo 1) {
if (arr[j - 1][k.toInt()] == 0) {
r += ten.pow(j)
k = mod(k - ten.pow(j), nBi)
}
}
if (k.compareTo(BigInteger.ONE) == 0) {
r += BigInteger.ONE
}
return r
}
private fun mod(m: BigInteger, n: BigInteger): BigInteger {
var result = m.mod(n)
if (result < BigInteger.ZERO) {
result += n
}
return result
}</syntaxhighlight>
{{out}}
<pre>A004290(1) = 1 = 1 * 1
A004290(2) = 10 = 2 * 5
A004290(3) = 111 = 3 * 37
A004290(4) = 100 = 4 * 25
A004290(5) = 10 = 5 * 2
A004290(6) = 1110 = 6 * 185
A004290(7) = 1001 = 7 * 143
A004290(8) = 1000 = 8 * 125
A004290(9) = 111111111 = 9 * 12345679
A004290(10) = 10 = 10 * 1
A004290(95) = 110010 = 95 * 1158
A004290(96) = 11100000 = 96 * 115625
A004290(97) = 11100001 = 97 * 114433
A004290(98) = 11000010 = 98 * 112245
A004290(99) = 111111111111111111 = 99 * 1122334455667789
A004290(100) = 100 = 100 * 1
A004290(101) = 101 = 101 * 1
A004290(102) = 1000110 = 102 * 9805
A004290(103) = 11100001 = 103 * 107767
A004290(104) = 1001000 = 104 * 9625
A004290(105) = 101010 = 105 * 962
A004290(297) = 1111011111111111111 = 297 * 3740778151889263
A004290(576) = 111111111000000 = 576 * 192901234375
A004290(594) = 11110111111111111110 = 594 * 18703890759446315
A004290(891) = 1111111111111111011 = 891 * 1247038284075321
A004290(909) = 1011111111111111111 = 909 * 1112333455567779
A004290(999) = 111111111111111111111111111 = 999 * 111222333444555666777889
A004290(1998) = 1111111111111111111111111110 = 1998 * 556111667222778333889445
A004290(2079) = 1001101101111111111111 = 2079 * 481530111164555609
A004290(2251) = 101101101111 = 2251 * 44913861
A004290(2277) = 11110111111111111011 = 2277 * 4879275850290343
A004290(2439) = 10000101011110111101111111 = 2439 * 4100082415379299344449
A004290(2997) = 1111110111111111111111111111 = 2997 * 370740777814851888925963
A004290(4878) = 100001010111101111011111110 = 4878 * 20500412076896496722245</pre>
=={{header|Lua}}==
Without a bignum library, some values do not display or calculate properly
{{trans|Ruby}}
<syntaxhighlight lang="lua">function array1D(n, v)
local tbl = {}
for i=1,n do
table.insert(tbl, v)
end
return tbl
end
function array2D(h, w, v)
local tbl = {}
for i=1,h do
table.insert(tbl, array1D(w, v))
end
return tbl
end
function mod(m, n)
m = math.floor(m)
local result = m % n
if result < 0 then
result = result + n
end
return result
end
function getA004290(n)
if n == 1 then
return 1
end
local arr = array2D(n, n, 0)
arr[1][1] = 1
arr[1][2] = 1
local m = 0
while true do
m = m + 1
if arr[m][mod(-10 ^ m, n) + 1] == 1 then
break
end
arr[m + 1][1] = 1
for k = 1, n - 1 do
arr[m + 1][k + 1] = math.max(arr[m][k + 1], arr[m][mod(k - 10 ^ m, n) + 1])
end
end
local r = 10 ^ m
local k = mod(-r, n)
for j = m - 1, 1, -1 do
if arr[j][k + 1] == 0 then
r = r + 10 ^ j
k = mod(k - 10 ^ j, n)
end
end
if k == 1 then
r = r + 1
end
return r
end
function test(cases)
for _,n in ipairs(cases) do
local result = getA004290(n)
print(string.format("A004290(%d) = %s = %d * %d", n, math.floor(result), n, math.floor(result / n)))
end
end
test({1, 2, 3, 4, 5, 6, 7, 8, 9})
test({95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105})
test({297, 576, 594, 891, 909, 999})
--test({1998, 2079, 2251, 2277})
--test({2439, 2997, 4878})</syntaxhighlight>
{{out}}
<pre>A004290(1) = 1 = 1 * 1
A004290(2) = 10 = 2 * 5
A004290(3) = 111 = 3 * 37
A004290(4) = 100 = 4 * 25
A004290(5) = 10 = 5 * 2
A004290(6) = 1110 = 6 * 185
A004290(7) = 1001 = 7 * 143
A004290(8) = 1000 = 8 * 125
A004290(9) = 111111111 = 9 * 12345679
A004290(95) = 110010 = 95 * 1158
A004290(96) = 11100000 = 96 * 115625
A004290(97) = 11100001 = 97 * 114433
A004290(98) = 11000010 = 98 * 112245
A004290(99) = 111011111111111120 = 99 * 1121324354657688 <-- error
A004290(100) = 100 = 100 * 1
A004290(101) = 101 = 101 * 1
A004290(102) = 1000110 = 102 * 9805
A004290(103) = 11100001 = 103 * 107767
A004290(104) = 1001000 = 104 * 9625
A004290(105) = 101010 = 105 * 962
A004290(297) = 111011111111111120 = 297 * 373774784885896 <-- error
A004290(576) = 111111111000000 = 576 * 192901234375
A004290(594) = 1.0100010101011e+19 = 594 * 17003384008436194 <-- error
A004290(891) = 111111111111111024 = 891 * 124703828407532 <-- error
A004290(909) = 1011110111111111168 = 909 * 1112332355457768 <-- error
A004290(999) = 1.1000000001001e+19 = 999 * 11011011012013022 <-- error</pre>
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">ClearAll[B10]
B10[n_Integer] := Module[{i, out},
i = 1;
While[! Divisible[FromDigits[IntegerDigits[i, 2], 10], n],
i++;
];
out = FromDigits[IntegerDigits[i, 2], 10];
Row@{n, " x ", out/n, " = ", out}
]
Table[B10[i], {i, Range[10]}] // Column
Table[B10[i], {i, 95, 105}] // Column
B10[297]
B10[576]
B10[594]
B10[891]
B10[909]
B10[999]
B10[1998]
B10[2079]
B10[2251]
B10[2277]
B10[2439]
B10[2997]
B10[4878]</syntaxhighlight>
{{out}}
<pre>1 x 1 = 1
2 x 5 = 10
3 x 37 = 111
4 x 25 = 100
5 x 2 = 10
6 x 185 = 1110
7 x 143 = 1001
8 x 125 = 1000
9 x 12345679 = 111111111
10 x 1 = 10
95 x 1158 = 110010
96 x 115625 = 11100000
97 x 114433 = 11100001
98 x 112245 = 11000010
99 x 1122334455667789 = 111111111111111111
100 x 1 = 100
101 x 1 = 101
102 x 9805 = 1000110
103 x 107767 = 11100001
104 x 9625 = 1001000
105 x 962 = 101010
297 x 3740778151889263 = 1111011111111111111
576 x 192901234375 = 111111111000000
594 x 18703890759446315 = 11110111111111111110
891 x 1247038284075321 = 1111111111111111011
909 x 1112333455567779 = 1011111111111111111
999 x 111222333444555666777889 = 111111111111111111111111111
1998 x 556111667222778333889445 = 1111111111111111111111111110
2079 x 481530111164555609 = 1001101101111111111111
2251 x 44913861 = 101101101111
2277 x 4879275850290343 = 11110111111111111011
2439 x 4100082415379299344449 = 10000101011110111101111111
2997 x 370740777814851888925963 = 1111110111111111111111111111
4878 x 20500412076896496722245 = 100001010111101111011111110</pre>
=={{header|Modula-2}}==
{{works with|TopSpeed (JPI) Modula-2 under DOSBox-X}}
Same approach as the math.stackexchange post referenced in the task description. This solution doesn't calculate the B10 itself; see the comment in the definition module.
<syntaxhighlight lang="modula2">
DEFINITION MODULE B10AsBin;
(* Returns a number representing the B10 of the passed-in number N.
The result has the same binary digits as B10 has decimal digits,
e.g. N = 7 returns 9 = 1001 binary, representing 1001 decimal.
Returns 0 if the procedure fails.
In TopSpeed Modula-2, CARDINAL is unsigned 16-bit, LONGCARD is unsigned 32-bit.
*)
PROCEDURE CalcB10AsBinary( N : CARDINAL) : LONGCARD;
END B10AsBin.
</syntaxhighlight>
<syntaxhighlight lang="modula2">
IMPLEMENTATION MODULE B10AsBin;
FROM Storage IMPORT ALLOCATE, DEALLOCATE;
PROCEDURE CalcB10AsBinary( N : CARDINAL) : LONGCARD;
CONST
MaxPower2 = 80000000H; (* 2^31 *)
VAR
pSums : POINTER TO ARRAY CARDINAL OF CARDINAL;
pWhen : POINTER TO ARRAY CARDINAL OF LONGCARD;
b10bin, pwr2 : LONGCARD;
j, j_stop, nrSums, res10, s : CARDINAL;
BEGIN
IF (N <= 1) THEN RETURN LONGCARD(N) END; (* dispose of trivial cases *)
(* TopSpeed Modula-2 doesn't seem to have dynamic arrays,
so we use a workaround *)
ALLOCATE( pSums, N*SIZE(CARDINAL));
ALLOCATE( pWhen, N*SIZE(LONGCARD));
FOR j := 0 TO N - 1 DO pWhen^[j] := 0; END;
b10bin := 0; (* result := 0; gets overwritten if procedure succeeds *)
res10 := 1; pwr2 := 1;
pSums^[0] := 0; pSums^[1] := 1; nrSums := 2;
pWhen^[1] := 1; (* record first occurrence of sum = 1 mod N *)
REPEAT
res10 := 10*res10 MOD N; pwr2 := 2*pwr2;
j := 0; j_stop := nrSums;
REPEAT
(* Possible new sums created by addition of res10 *)
s := pSums^[j] + res10;
IF (s >= N) THEN DEC(s, N); END; (* take sums mod N *)
IF (pWhen^[s] = 0) THEN (* if we haven't had this sum already *)
pWhen^[s] := pWhen^[pSums^[j]] + pwr2; (* record first occurrence *)
IF (s = 0) THEN b10bin := pWhen^[0]; (* if s = 0 then done *)
ELSE
pSums^[nrSums] := s; INC( nrSums); (* else store the sum s *)
END;
END;
INC(j);
UNTIL (j = j_stop) OR (b10bin > 0);
UNTIL (pwr2 = MaxPower2) OR (b10bin > 0);
DEALLOCATE( pSums, N*SIZE(CARDINAL));
DEALLOCATE( pWhen, N*SIZE(LONGCARD));
RETURN b10bin;
END CalcB10AsBinary;
END B10AsBin.
</syntaxhighlight>
<syntaxhighlight lang="modula2">
MODULE B10Demo;
FROM B10AsBin IMPORT CalcB10AsBinary;
IMPORT IO;
FROM Str IMPORT CardToStr;
CONST NrValues = 34;
TYPE DemoValues = ARRAY [1..NrValues] OF CARDINAL;
VAR
values : DemoValues;
b10 : LONGCARD;
j : CARDINAL;
b10Str : ARRAY [0..31] OF CHAR;
ok : BOOLEAN;
BEGIN
values := DemoValues( 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105,
297, 576, 594, 891, 909, 999, 1998, 2079, 2251, 2277, 2439, 2997, 4878);
FOR j := 1 TO NrValues DO
b10 := CalcB10AsBinary( values[j]);
CardToStr( b10, b10Str, 2, ok);
IO.WrCard( values[j], 4); IO.WrStr( ' ');
IO.WrStr( b10Str); IO.WrLn;
END;
END B10Demo.
</syntaxhighlight>
{{out}}
<pre>
1 1
2 10
3 111
4 100
5 10
6 1110
7 1001
8 1000
9 111111111
10 10
95 110010
96 11100000
97 11100001
98 11000010
99 111111111111111111
100 100
101 101
102 1000110
103 11100001
104 1001000
105 101010
297 1111011111111111111
576 111111111000000
594 11110111111111111110
891 1111111111111111011
909 1011111111111111111
999 111111111111111111111111111
1998 1111111111111111111111111110
2079 1001101101111111111111
2251 101101101111
2277 11110111111111111011
2439 10000101011110111101111111
2997 1111110111111111111111111111
4878 100001010111101111011111110
</pre>
=={{header|Nim}}==
{{trans|Phix}}
{{libheader|bignum}}
As in the Phix solution the big integer library is only used to check the result and compute the factor.
<syntaxhighlight lang="nim">import sequtils, strformat, strutils, times
import bignum
func b10(n: int64): string =
doAssert n > 0, "Argument must be positive."
if n == 1: return "1"
var pow, val = newSeq[int64](n + 1)
var ten = 1i64
for x in 1i64..val.high:
val[x] = ten
for i in 0..n:
if pow[i] != 0 and pow[i] != x:
let k = (ten + i) mod n
if pow[k] == 0: pow[k] = x
if pow[ten] == 0: pow[ten] = x
ten = 10 * ten mod n
if pow[0] != 0: break
if pow[0] == 0:
raise newException(ValueError, "Can’t do it!")
# Build result string.
var x = n
var count = 0'i64
while x != 0:
let pm = pow[x mod n]
if count > pm:
result.add repeat('0', count - pm)
count = pm - 1
result.add '1'
x = (n + x - val[pm]) mod n
result.add repeat('0', count)
const Data = toSeq(1..10) & toSeq(95..105) &
@[297, 576, 594, 891, 909, 999, 1998, 2079, 2251, 2277, 2439, 2997, 4878]
let t0 = cpuTime()
for val in Data:
let res = b10(val)
let m = newInt(res)
if m mod val != 0: echo &"Wrong result for {val}."
else: echo &"{val:4} × {m div val:<24} = {res}"
echo &"Time: {cpuTime() - t0:.3f} s"</syntaxhighlight>
{{out}}
<pre> 1 × 1 = 1
2 × 5 = 10
3 × 37 = 111
4 × 25 = 100
5 × 2 = 10
6 × 185 = 1110
7 × 143 = 1001
8 × 125 = 1000
9 × 12345679 = 111111111
10 × 1 = 10
95 × 1158 = 110010
96 × 115625 = 11100000
97 × 114433 = 11100001
98 × 112245 = 11000010
99 × 1122334455667789 = 111111111111111111
100 × 1 = 100
101 × 1 = 101
102 × 9805 = 1000110
103 × 107767 = 11100001
104 × 9625 = 1001000
105 × 962 = 101010
297 × 3740778151889263 = 1111011111111111111
576 × 192901234375 = 111111111000000
594 × 18703890759446315 = 11110111111111111110
891 × 1247038284075321 = 1111111111111111011
909 × 1112333455567779 = 1011111111111111111
999 × 111222333444555666777889 = 111111111111111111111111111
1998 × 556111667222778333889445 = 1111111111111111111111111110
2079 × 481530111164555609 = 1001101101111111111111
2251 × 44913861 = 101101101111
2277 × 4879275850290343 = 11110111111111111011
2439 × 4100082415379299344449 = 10000101011110111101111111
2997 × 370740777814851888925963 = 1111110111111111111111111111
4878 × 20500412076896496722245 = 100001010111101111011111110
Time: 0.005 s</pre>
=={{header|Pascal}}==
Line 576 ⟶ 2,204:
gmp is only used, to get the multiplier.<BR>
Now lightning fast
<
//numbers having only digits 0 and 1 in their decimal representation
//see https://oeis.org/A004290
Line 728 ⟶ 2,356:
check_B10(9999);
check_B10(2*9999); //real 0m0,077s :-)
end.</
{{out}}
<pre style="height:150px; overflow: auto;">
Line 772 ⟶ 2,400:
===Brutish and short===
Brute-force code does the task minimum, but slowly (and that even with a short-circuit to avoid calculations for 99, 999)
<
use warnings;
use Math::AnyNum qw(:overload as_bin digits2num);
Line 781 ⟶ 2,409:
else { while (1) { last unless as_bin(++$y) % $x } }
printf "%4d: %28s %s\n", $x, as_bin($y), as_bin($y)/$x;
}</
{{out}}
<pre style="height:30ex"> 1: 1 1
Line 814 ⟶ 2,442:
Much faster, while also completing stretch goal.
{{trans|Sidef}}
<
use warnings;
use Math::AnyNum qw(:overload powmod);
Line 844 ⟶ 2,472:
my $a = B10($n);
printf "%6d: %28s %s\n", $n, $a, $a/$n;
}</
{{out}}
<pre style="height:30ex"> 1: 1 1
Line 883 ⟶ 2,511:
=={{header|Phix}}==
Using the Ed Pegg Jr 'Binary' Puzzle algorithm as linked to by OEIS.<br>
Very fast, finishes near-instantly, only needs/uses gmp to validate the results.<br>
You can run this online [http://phix.x10.mx/p2js/b10.htm here].
<!--<syntaxhighlight lang="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;">b10</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">then</span> <span style="color: #008080;">return</span> <span style="color: #008000;">"1"</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">NUM</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">count</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">ten</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">x</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">val</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">NUM</span><span style="color: #0000FF;">),</span>
<span style="color: #000000;">pow</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">NUM</span><span style="color: #0000FF;">)</span>
<span style="color: #000080;font-style:italic;">--
-- Calculate each 10^k mod n, along with all possible sums that can be
-- made from it and
--
-- ten/val[]
--
-- 10^1 = 10 mod 7 = 3. Possible sums: 1 3 4
--
--
--
-- ie since 10^3 mod 7 + 10^0 mod 7 == (6+1) mod 7 == 0, we know that
--
--
--
-- Another example is 10^k mod 9 is 1 for all k. Hence we need to find
-- 9 different ways to generate a 1, before we get a sum (mod 9) of 0,
-- and hence b10(9) is 111111111, ie 10^8+10^7+..+10^0, and obviously
-- 9 iterations is somewhat less than all interims 11..111111110.
--</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">x</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">n</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">val</span><span style="color: #0000FF;">[</span><span style="color: #000000;">x</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">ten</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">j</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">NUM</span> <span style="color: #008080;">do</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">pow</span><span style="color: #0000FF;">[</span><span style="color: #000000;">j</span><span style="color: #0000FF;">]</span> <span style="color: #008080;">and</span> <span style="color: #000000;">pow</span><span style="color: #0000FF;">[</span><span style="color: #000000;">j</span><span style="color: #0000FF;">]!=</span><span style="color: #000000;">x</span> <span style="color: #008080;">then</span> <span style="color: #000080;font-style:italic;">-- (j seen, in a prior iteration)</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">k</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mod</span><span style="color: #0000FF;">(</span><span style="color: #000000;">j</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">+</span><span style="color: #000000;">ten</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)+</span><span style="color: #000000;">1</span>
<span style="color: #008080;">if</span> <span style="color: #008080;">not</span> <span style="color: #000000;">pow</span><span style="color: #0000FF;">[</span><span style="color: #000000;">k</span><span style="color: #0000FF;">]</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">pow</span><span style="color: #0000FF;">[</span><span style="color: #000000;">k</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">x</span> <span style="color: #000080;font-style:italic;">-- (k was seen in this iteration)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">if</span> <span style="color: #008080;">not</span> <span style="color: #000000;">pow</span><span style="color: #0000FF;">[</span><span style="color: #000000;">ten</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span> <span style="color: #008080;">then</span> <span style="color: #000000;">pow</span><span style="color: #0000FF;">[</span><span style="color: #000000;">ten</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">x</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #000000;">ten</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mod</span><span style="color: #0000FF;">(</span><span style="color: #000000;">10</span><span style="color: #0000FF;">*</span><span style="color: #000000;">ten</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">pow</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span> <span style="color: #008080;">then</span> <span style="color: #008080;">exit</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">pow</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span> <span style="color: #7060A8;">crash</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"Can't do it!"</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #000080;font-style:italic;">--
-- Fairly obviously (for b10(7)) we need the 10^3, then we will need
-- a sum of 1, which first appeared for 10^0, after which we are done.
-- The required answer is therefore 1001, ie 10^3 + 10^0.
<span style="color: #004080;">string</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">""</span>
<span style="color: #000000;">x</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">n</span>
<span style="color: #008080;">while</span> <span style="color: #000000;">x</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">pm</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">pow</span><span style="color: #0000FF;">[</span><span style="color: #7060A8;">mod</span><span style="color: #0000FF;">(</span><span style="color: #000000;">x</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">count</span><span style="color: #0000FF;">></span><span style="color: #000000;">pm</span> <span style="color: #008080;">then</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">&=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #008000;">'0'</span><span style="color: #0000FF;">,</span><span style="color: #000000;">count</span><span style="color: #0000FF;">-</span><span style="color: #000000;">pm</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #000000;">count</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">pm</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">;</span>
<span style="color: #000000;">res</span> <span style="color: #0000FF;">&=</span> <span style="color: #008000;">'1'</span>
<span style="color: #000000;">x</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mod</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">+</span><span style="color: #000000;">x</span><span style="color: #0000FF;">-</span><span style="color: #000000;">val</span><span style="color: #0000FF;">[</span><span style="color: #000000;">pm</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">],</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<span style="color: #000000;">res</span> <span style="color: #0000FF;">&=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #008000;">'0'</span><span style="color: #0000FF;">,</span><span style="color: #000000;">count</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">res</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">tests</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">10</span><span style="color: #0000FF;">)&</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">105</span><span style="color: #0000FF;">,</span><span style="color: #000000;">95</span><span style="color: #0000FF;">)&{</span><span style="color: #000000;">297</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">576</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">891</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">909</span><span style="color: #0000FF;">,</span>
<span style="color: #000000;">999</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1998</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">2079</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">2251</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">2277</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">2439</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">2997</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">4878</span><span style="color: #0000FF;">}</span>
<span style="color: #008080;">include</span> <span style="color: #004080;">mpfr</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
<span style="color: #004080;">mpz</span> <span style="color: #000000;">m10</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpz_init</span><span style="color: #0000FF;">()</span>
<span style="color: #004080;">atom</span> <span style="color: #000000;">t0</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">time</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">tests</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">ti</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">tests</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">r10</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">b10</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ti</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">mpz_set_str</span><span style="color: #0000FF;">(</span><span style="color: #000000;">m10</span><span style="color: #0000FF;">,</span><span style="color: #000000;">r10</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">mpz_fdiv_q_ui</span><span style="color: #0000FF;">(</span><span style="color: #000000;">m10</span><span style="color: #0000FF;">,</span><span style="color: #000000;">m10</span><span style="color: #0000FF;">,</span><span style="color: #000000;">ti</span><span style="color: #0000FF;">)!=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span> <span style="color: #000000;">r10</span> <span style="color: #0000FF;">&=</span> <span style="color: #008000;">" ??"</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%4d * %-24s = %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">ti</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">mpz_get_str</span><span style="color: #0000FF;">(</span><span style="color: #000000;">m10</span><span style="color: #0000FF;">),</span><span style="color: #000000;">r10</span><span style="color: #0000FF;">})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</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>
<!--</syntaxhighlight>-->
{{out}}
<pre>
Line 991 ⟶ 2,623:
"0.1s"
</pre>
=={{header|Python}}==
{{trans|Kotlin}}
<syntaxhighlight lang="python">def getA004290(n):
if n < 2:
return 1
arr = [[0 for _ in range(n)] for _ in range(n)]
arr[0][0] = 1
arr[0][1] = 1
m = 0
while True:
m += 1
if arr[m - 1][-10 ** m % n] == 1:
break
arr[m][0] = 1
for k in range(1, n):
arr[m][k] = max([arr[m - 1][k], arr[m - 1][k - 10 ** m % n]])
r = 10 ** m
k = -r % n
for j in range((m - 1), 0, -1):
if arr[j - 1][k] == 0:
r = r + 10 ** j
k = (k - 10 ** j) % n
if k == 1:
r += 1
return r
for n in [i for i in range(1, 11)] + \
[i for i in range(95, 106)] + \
[297, 576, 594, 891, 909, 999, 1998, 2079, 2251, 2277, 2439, 2997, 4878]:
result = getA004290(n)
print(f"A004290({n}) = {result} = {n} * {result // n})")
</syntaxhighlight>{{out}} Same as Kotlin.
=={{header|Raku}}==
Line 997 ⟶ 2,663:
Naive, brute force. Simplest thing that could possibly work. Will find any B10 eventually (until you run out of memory or patience) but sloooow, especially for larger multiples of 9.
<syntaxhighlight lang="raku"
Based on [http://www.mathpuzzle.com/Binary.html Ed Pegg jr.s C code] from Mathpuzzlers.com. Similar to Phix and Go entries.
<syntaxhighlight lang="raku"
return 1 if n == 1;
my ($count, $power-mod-n) = 0, 1;
Line 1,031 ⟶ 2,697:
for flat 1..10, 95..105, 297, 576, 594, 891, 909, 999, 1998, 2079, 2251, 2277, 2439, 2997, 4878 {
printf "%6d: %28s %s\n", $_, my $a = Ed-Pegg-jr($_), $a / $_;
}</
{{out}}
<pre>Number: B10 Multiplier
Line 1,070 ⟶ 2,736:
=={{header|REXX}}==
<
numeric digits 30; w= length( commas( copies(1, digits()))) /*
parse arg list
if list=='' then list= 1..10 95..105 297
say center(' N ', 9, "─") center(' B10 ', w, "─") center(' multiplier ', w, "─")
do i=1 for words(list)
z= word(list, i); LO= z; HI= z
if pos(.., z)\==0 then parse var z LO '..' HI
do n=LO to HI; m= B10(n)
say right(commas(n), 9) right(commas(n*m), w) left(commas(m), w)
end /*n*/
end /*i*/
exit
/*──────────────────────────────────────────────────────────────────────────────────────*/
commas: parse arg _; do c=length(_)-3 to 1 by -3; _= insert(',', _, c); end; return _
/*──────────────────────────────────────────────────────────────────────────────────────*/
B10: parse arg
select
when verify(
when verify(
do k= 1 for 8
start= start || copies(k, L)
end /*k*/
end
when
when right(z, 1)==7 then do; start=3; inc= 10; end
otherwise nop
Line 1,102 ⟶ 2,768:
q= length(start)
if q>digits() then numeric digits q
do m=start by inc until verify(p, 10)==0;
end /*m*/
return m</
{{out|output|text= when using the default inputs:}}
<pre>
Line 1,138 ⟶ 2,804:
999 111,111,111,111,111,111,111,111,111 111,222,333,444,555,666,777,889
</pre>
=={{header|Ring}}==
<syntaxhighlight lang="ring">
see "working..." + nl
see "Minimum positive multiple in base 10 using only 0 and 1:" + nl
limit1 = 1000
limit2 = 111222333444555666777889
plusflag = 0
plus = [297,576,594,891,909,999]
for n = 1 to limit1
if n = 106
plusflag = 1
nplus = 0
ok
lenplus = len(plus)
if plusflag = 1
nplus = nplus + 1
if nplus < lenplus+1
n = plus[nplus]
else
exit
ok
ok
for m = 1 to limit2
flag = 1
prod = n*m
pstr = string(prod)
for p = 1 to len(pstr)
if not(pstr[p] = "0" or pstr[p] = "1")
flag = 0
exit
ok
next
if flag = 1
see "" + n + " * " + m + " = " + pstr + nl
exit
ok
next
if n = 10
n = 94
ok
next
see "done..." + nl
</syntaxhighlight>
{{out}}
<pre>
working...
Minimum positive multiple in base 10 using only 0 and 1:
1 * 1 = 1
2 * 5 = 10
3 * 37 = 111
4 * 25 = 100
5 * 2 = 10
6 * 185 = 1110
7 * 143 = 1001
8 * 125 = 1000
9 * 12345679 = 111111111
10 * 1 = 10
95 * 1158 = 110010
96 * 115625 = 11100000
97 * 114433 = 11100001
98 * 112245 = 11000010
99 * 1122334455667789 = 111111111111111111
100 * 1 = 100
101 * 1 = 101
102 * 9805 = 1000110
103 * 107767 = 11100001
104 * 9625 = 1001000
105 * 962 = 101010
297 * 3740778151889263 = 1111011111111111111
576 * 192901234375 = 111111111000000
594 * 18703890759446315 = 11110111111111111110
891 * 1247038284075321 = 1111111111111111011
909 * 1112333455567779 = 1011111111111111111
999 * 111222333444555666777889 = 111111111111111111111111111
done...
</pre>
=={{header|Ruby}}==
{{trans|Kotlin}}
<syntaxhighlight lang="ruby">def mod(m, n)
result = m % n
if result < 0 then
result = result + n
end
return result
end
def getA004290(n)
if n == 1 then
return 1
end
arr = Array.new(n) { Array.new(n, 0) }
arr[0][0] = 1
arr[0][1] = 1
m = 0
while true
m = m + 1
if arr[m - 1][mod(-10 ** m, n)] == 1 then
break
end
arr[m][0] = 1
for k in 1 .. n - 1
arr[m][k] = [arr[m - 1][k], arr[m - 1][mod(k - 10 ** m, n)]].max
end
end
r = 10 ** m
k = mod(-r, n)
(m - 1).downto(1) { |j|
if arr[j - 1][k] == 0 then
r = r + 10 ** j
k = mod(k - 10 ** j, n)
end
}
if k == 1 then
r = r + 1
end
return r
end
testCases = Array(1 .. 10)
testCases.concat(Array(95 .. 105))
testCases.concat([297, 576, 594, 891, 909, 999, 1998, 2079, 2251, 2277, 2439, 2997, 4878])
for n in testCases
result = getA004290(n)
print "A004290(%d) = %d = %d * %d\n" % [n, result, n, result / n]
end</syntaxhighlight>
{{out}}
<pre>A004290(1) = 1 = 1 * 1
A004290(2) = 10 = 2 * 5
A004290(3) = 111 = 3 * 37
A004290(4) = 100 = 4 * 25
A004290(5) = 10 = 5 * 2
A004290(6) = 1110 = 6 * 185
A004290(7) = 1001 = 7 * 143
A004290(8) = 1000 = 8 * 125
A004290(9) = 111111111 = 9 * 12345679
A004290(10) = 10 = 10 * 1
A004290(95) = 110010 = 95 * 1158
A004290(96) = 11100000 = 96 * 115625
A004290(97) = 11100001 = 97 * 114433
A004290(98) = 11000010 = 98 * 112245
A004290(99) = 111111111111111111 = 99 * 1122334455667789
A004290(100) = 100 = 100 * 1
A004290(101) = 101 = 101 * 1
A004290(102) = 1000110 = 102 * 9805
A004290(103) = 11100001 = 103 * 107767
A004290(104) = 1001000 = 104 * 9625
A004290(105) = 101010 = 105 * 962
A004290(297) = 1111011111111111111 = 297 * 3740778151889263
A004290(576) = 111111111000000 = 576 * 192901234375
A004290(594) = 11110111111111111110 = 594 * 18703890759446315
A004290(891) = 1111111111111111011 = 891 * 1247038284075321
A004290(909) = 1011111111111111111 = 909 * 1112333455567779
A004290(999) = 111111111111111111111111111 = 999 * 111222333444555666777889
A004290(1998) = 1111111111111111111111111110 = 1998 * 556111667222778333889445
A004290(2079) = 1001101101111111111111 = 2079 * 481530111164555609
A004290(2251) = 101101101111 = 2251 * 44913861
A004290(2277) = 11110111111111111011 = 2277 * 4879275850290343
A004290(2439) = 10000101011110111101111111 = 2439 * 4100082415379299344449
A004290(2997) = 1111110111111111111111111111 = 2997 * 370740777814851888925963
A004290(4878) = 100001010111101111011111110 = 4878 * 20500412076896496722245</pre>
=={{header|Scala}}==
{{trans|Java}}
<syntaxhighlight lang="scala">import scala.collection.mutable.ListBuffer
object MinimumNumberOnlyZeroAndOne {
def main(args: Array[String]): Unit = {
for (n <- getTestCases) {
val result = getA004290(n)
println(s"A004290($n) = $result = $n * ${result / n}")
}
}
def getTestCases: List[Int] = {
val testCases = ListBuffer.empty[Int]
for (i <- 1 to 10) {
testCases += i
}
for (i <- 95 to 105) {
testCases += i
}
for (i <- Array(297, 576, 594, 891, 909, 999, 1998, 2079, 2251, 2277, 2439, 2997, 4878)) {
testCases += i
}
testCases.toList
}
def getA004290(n: Int): BigInt = {
if (n == 1) {
return 1
}
val L = Array.ofDim[Int](n, n)
for (i <- 2 until n) {
L(0)(i) = 0
}
L(0)(0) = 1
L(0)(1) = 1
var m = 0
val ten = BigInt(10)
val nBi = BigInt(n)
var loop = true
while (loop) {
m = m + 1
if (L(m - 1)(mod(-ten.pow(m), nBi).intValue()) == 1) {
loop = false
} else {
L(m)(0) = 1
for (k <- 1 until n) {
L(m)(k) = math.max(L(m - 1)(k), L(m - 1)(mod(BigInt(k) - ten.pow(m), nBi).toInt))
}
}
}
var r = ten.pow(m)
var k = mod(-r, nBi)
for (j <- m - 1 to 1 by -1) {
if (L(j - 1)(k.toInt) == 0) {
r = r + ten.pow(j)
k = mod(k - ten.pow(j), nBi)
}
}
if (k == 1) {
r = r + 1
}
r
}
def mod(m: BigInt, n: BigInt): BigInt = {
var result = m % n
if (result < 0) {
result = result + n
}
result
}
}</syntaxhighlight>
{{out}}
<pre>A004290(1) = 1 = 1 * 1
A004290(2) = 10 = 2 * 5
A004290(3) = 111 = 3 * 37
A004290(4) = 100 = 4 * 25
A004290(5) = 10 = 5 * 2
A004290(6) = 1110 = 6 * 185
A004290(7) = 1001 = 7 * 143
A004290(8) = 1000 = 8 * 125
A004290(9) = 111111111 = 9 * 12345679
A004290(10) = 10 = 10 * 1
A004290(95) = 110010 = 95 * 1158
A004290(96) = 11100000 = 96 * 115625
A004290(97) = 11100001 = 97 * 114433
A004290(98) = 11000010 = 98 * 112245
A004290(99) = 111111111111111111 = 99 * 1122334455667789
A004290(100) = 100 = 100 * 1
A004290(101) = 101 = 101 * 1
A004290(102) = 1000110 = 102 * 9805
A004290(103) = 11100001 = 103 * 107767
A004290(104) = 1001000 = 104 * 9625
A004290(105) = 101010 = 105 * 962
A004290(297) = 1111011111111111111 = 297 * 3740778151889263
A004290(576) = 111111111000000 = 576 * 192901234375
A004290(594) = 11110111111111111110 = 594 * 18703890759446315
A004290(891) = 1111111111111111011 = 891 * 1247038284075321
A004290(909) = 1011111111111111111 = 909 * 1112333455567779
A004290(999) = 111111111111111111111111111 = 999 * 111222333444555666777889
A004290(1998) = 1111111111111111111111111110 = 1998 * 556111667222778333889445
A004290(2079) = 1001101101111111111111 = 2079 * 481530111164555609
A004290(2251) = 101101101111 = 2251 * 44913861
A004290(2277) = 11110111111111111011 = 2277 * 4879275850290343
A004290(2439) = 10000101011110111101111111 = 2439 * 4100082415379299344449
A004290(2997) = 1111110111111111111111111111 = 2997 * 370740777814851888925963
A004290(4878) = 100001010111101111011111110 = 4878 * 20500412076896496722245</pre>
=={{header|Sidef}}==
Based on the [https://oeis.org/A004290/a004290_1.sage.txt Sage code by Eric M. Schmidt], which in turn is based on [http://www.mathpuzzle.com/Binary.html C code by Rick Heylen].
<
return 0 if (n == 0)
Line 1,174 ⟶ 3,114:
for n in (1..10, 95..105, 297, 576, 594, 891, 909, 999, 1998, 2079, 2251, 2277, 2439, 2997, 4878) {
printf("%6d: %28s %s\n", n, var a = find_B10(n), a/n)
}</
{{out}}
<pre>
Line 1,216 ⟶ 3,156:
=={{header|Tcl}}==
I have written this code from scratch in order to understand what is going on. Finally my code is quite similar to some other entries.
<syntaxhighlight lang="tcl">
package require Tcl 8.5
Line 1,313 ⟶ 3,253:
do_1 $n
}
</syntaxhighlight>
{{out}}
<pre>
Line 1,352 ⟶ 3,292:
</pre>
Time: ~ 0.060 sec
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<syntaxhighlight lang="vbnet">Imports System, System.Linq, System.Collections.Generic, System.Console
Module Module1
Dim fmt As String = "{0,4} * {1,31:n0} = {2,-28}" & vbLf
Sub B10(ByVal n As Integer)
If n <= 1 Then Return
Dim pow As Integer() = New Integer(n) {},
val As Integer() = New Integer(n) {},
count As Integer = 0, ten As Integer = 1, x As Integer = 1
While x <= n : val(x) = ten : For j As Integer = 0 To n
If pow(j) <> 0 AndAlso pow((j + ten) Mod n) = 0 AndAlso _
pow(j) <> x Then pow((j + ten) Mod n) = x
Next : If pow(ten) = 0 Then pow(ten) = x
ten = (10 * ten) Mod n : If pow(0) <> 0 Then
x = n : Dim s As String = "" : While x <> 0
Dim p As Integer = pow(x Mod n)
If count > p Then s += New String("0"c, count - p)
count = p - 1 : s += "1"
x = (n + x - val(p)) Mod n : End While
If count > 0 Then s += New String("0"c, count)
Write(fmt, n, Decimal.Parse(s) / n, s) : Return
End If : x += 1 : End While : Write("Can't do it!")
End Sub
Sub Main(ByVal args As String())
Dim st As DateTime = DateTime.Now
Dim m As List(Of Integer) = New List(Of Integer) From _
{297,576,594,891,909,999,1998,2079,2251,2277,2439,2997,4878}
Write(fmt, "n", "multiplier", "B10")
WriteLine(New String("-"c, 69)) : Write(fmt, 1, 1, 1)
For n As Integer = 1 To m.Last()
If n <= 10 OrElse n >= 95 AndAlso n <= 105 OrElse m.Contains(n) Then B10(n)
Next : WriteLine(vbLf & "Took {0}ms", (DateTime.Now - st).TotalMilliseconds)
End Sub
End Module</syntaxhighlight>
{{out}}
<pre> n * multiplier = B10
---------------------------------------------------------------------
1 * 1 = 1
2 * 5 = 10
3 * 37 = 111
4 * 25 = 100
5 * 2 = 10
6 * 185 = 1110
7 * 143 = 1001
8 * 125 = 1000
9 * 12,345,679 = 111111111
10 * 1 = 10
95 * 1,158 = 110010
96 * 115,625 = 11100000
97 * 114,433 = 11100001
98 * 112,245 = 11000010
99 * 1,122,334,455,667,789 = 111111111111111111
100 * 1 = 100
101 * 1 = 101
102 * 9,805 = 1000110
103 * 107,767 = 11100001
104 * 9,625 = 1001000
105 * 962 = 101010
297 * 3,740,778,151,889,263 = 1111011111111111111
576 * 192,901,234,375 = 111111111000000
594 * 18,703,890,759,446,315 = 11110111111111111110
891 * 1,247,038,284,075,321 = 1111111111111111011
909 * 1,112,333,455,567,779 = 1011111111111111111
999 * 111,222,333,444,555,666,777,889 = 111111111111111111111111111
1998 * 556,111,667,222,778,333,889,445 = 1111111111111111111111111110
2079 * 481,530,111,164,555,609 = 1001101101111111111111
2251 * 44,913,861 = 101101101111
2277 * 4,879,275,850,290,343 = 11110111111111111011
2439 * 4,100,082,415,379,299,344,449 = 10000101011110111101111111
2997 * 370,740,777,814,851,888,925,963 = 1111110111111111111111111111
4878 * 20,500,412,076,896,496,722,245 = 100001010111101111011111110
Took 21.863ms
</pre>
=={{header|Wren}}==
{{trans|Go}}
{{libheader|Wren-fmt}}
{{libheader|Wren-big}}
Very fast. Taking less than 40ms even in Wren.
<syntaxhighlight lang="wren">import "./fmt" for Fmt
import "./big" for BigInt
var b10 = Fn.new { |n|
if (n == 1) {
Fmt.print("$4d: $28s $-24d", 1, "1", 1)
return
}
var n1 = n + 1
var pow = List.filled(n1, 0)
var val = List.filled(n1, 0)
var count = 0
var ten = 1
var x = 1
while (x < n1) {
val[x] = ten
for (j in 0...n1) {
if (pow[j] != 0 && pow[(j+ten)%n] == 0 && pow[j] != x) pow[(j+ten)%n] = x
}
if (pow[ten] == 0) pow[ten] = x
ten = (10*ten) % n
if (pow[0] != 0) break
x = x + 1
}
x = n
if (pow[0] != 0) {
var s = ""
while (x != 0) {
var p = pow[x%n]
if (count > p) s = s + ("0" * (count-p))
count = p - 1
s = s + "1"
x = (n + x - val[p]) % n
}
if (count > 0) s = s + ("0" * count)
var mpm = BigInt.new(s)
var mul = mpm/n
Fmt.print("$4d: $28s $-24i", n, s, mul)
} else {
System.print("Can't do it!")
}
}
var start = System.clock
var tests = [
[1, 10], [95, 105], [297], [576], [594], [891], [909], [999],
[1998], [2079], [2251], [2277], [2439], [2997], [4878]
]
System.print(" n B10 multiplier")
System.print("----------------------------------------------")
for (test in tests) {
var from = test[0]
var to = from
if (test.count == 2) to = test[1]
for (n in from..to) b10.call(n)
}
System.print("\nTook %(System.clock-start) seconds.")</syntaxhighlight>
{{out}}
<pre>
n B10 multiplier
----------------------------------------------
1: 1 1
2: 10 5
3: 111 37
4: 100 25
5: 10 2
6: 1110 185
7: 1001 143
8: 1000 125
9: 111111111 12345679
10: 10 1
95: 110010 1158
96: 11100000 115625
97: 11100001 114433
98: 11000010 112245
99: 111111111111111111 1122334455667789
100: 100 1
101: 101 1
102: 1000110 9805
103: 11100001 107767
104: 1001000 9625
105: 101010 962
297: 1111011111111111111 3740778151889263
576: 111111111000000 192901234375
594: 11110111111111111110 18703890759446315
891: 1111111111111111011 1247038284075321
909: 1011111111111111111 1112333455567779
999: 111111111111111111111111111 111222333444555666777889
1998: 1111111111111111111111111110 556111667222778333889445
2079: 1001101101111111111111 481530111164555609
2251: 101101101111 44913861
2277: 11110111111111111011 4879275850290343
2439: 10000101011110111101111111 4100082415379299344449
2997: 1111110111111111111111111111 370740777814851888925963
4878: 100001010111101111011111110 20500412076896496722245
Took 0.037077 seconds.
</pre>
=={{header|zkl}}==
{{trans|Pascal}}
<
B10_4=10*10*10*10,
HexB10=T(0000,0001,0010,0011,0100,0101,0110,0111,
Line 1,378 ⟶ 3,503:
while(i<B10_MAX){ if(conv2B10( i+=1 )%n == 0) return(conv2B10(i)); }
return(-1); // overflow 64 bit signed int
}</
<
T(297, 576, 891, 909, 1998, 2079, 2251, 2277, 2439, 2997, 4878))){
foreach n in (r){
Line 1,386 ⟶ 3,511:
else println("B10(%4d) = %d = %d * %d".fmt(n,b10,n,b10/n));
}
}</
{{out}}
<pre style="height:45ex">
|