Minimum positive multiple in base 10 using only 0 and 1: Difference between revisions

Line 1,114:
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.
 
<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 )</lang>
{{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>
 
2,481

edits