Addition chains: Difference between revisions
m
syntax highlighting fixup automation
(→{{header|Haskell}}: added practical solution) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 26:
{{trans|Python}}
<
V chain = [0] * n
V in_chain = [0B] * (n + 1)
Line 61:
L(n) [7, 14, 21, 29, 32, 42, 64, 47, 79, 191, 382, 379]
V (best, cnt) = bauer(n)
print("L(#.) = #., count of minimum chain: #.\ne.g.: #.\n".format(n, best.len - 1, cnt, best))</
{{out}}
Line 105:
=={{header|C}}==
{{trans|Kotlin}}
<
#include <stdlib.h>
#include <string.h>
Line 300:
for (i = 0; i < 12; ++i) findBrauer(nums[i], 12, 79);
return 0;
}</
{{out}}
Line 386:
=={{header|C sharp|C#}}==
{{trans|Java}}
<
namespace AdditionChains {
Line 433:
}
}
}</
{{out}}
<pre>N = 7
Line 486:
While this worked, something made it run extremely slow.
{{trans|D}}
<
#include <tuple>
#include <vector>
Line 531:
return 0;
}</
{{out}}
<pre>
Line 584:
=={{header|D}}==
{{trans|Scala}}
<
import std.typecons;
Line 622:
find_brauer(i);
}
}</
{{out}}
<pre>N = 7
Line 673:
=={{header|EchoLisp}}==
<
;; 2^n
(define exp2 (build-vector 32 (lambda(i)(expt 2 i))))
Line 720:
(printf "L(%d) = %d - brauer-chains: %d non-brauer: %d chains: %a %a "
n *minlg* [*counts* 0] [*counts* 1] [*chains* 0] [*chains* 1]))
</syntaxhighlight>
{{out}}
<pre>
Line 743:
===Version 1===
{{trans|Kotlin}}
<
import "fmt"
Line 896:
findBrauer(num, 12, 79)
}
}</
{{out}}
Line 983:
{{trans|Phix}}
Much faster than Version 1 and can now complete the non-Brauer analysis for N > 79 in a reasonable time.
<
import (
Line 1,100:
}
fmt.Printf("\nTook %s\n", time.Since(start))
}</
{{out}}
Line 1,191:
=={{header|Groovy}}==
{{trans|Java}}
<
private static class Pair {
int f, s
Line 1,244:
}
}
}</
{{out}}
<pre>N = 7
Line 1,298:
Implementation using backtracking.
<
-- search strategies
Line 1,320:
isBrauer [_] = True
isBrauer [_,_] = True
isBrauer (x:y:xs) = (x - y) `elem` (y:xs) && isBrauer (y:xs)</
Usage examples
Line 1,340:
Tasks implementation
<
task n =
let ch = chains total n
Line 1,352:
printf "non-Brauer chains(%i)\t: count = %i\tEx: %s\n\n" n (length ch - length br) (show $ reverse $ head nbr)
else
putStrLn "No non Brauer chains\n"</
<pre>λ> mapM_ task [7,14,21,29,32,42,64]
Line 1,385:
For the extra task used compiled code, not GHCi.
<
extraTask n =
let ch = chains brauer n
Line 1,393:
putStrLn "Non-Brauer analysis suppressed\n"
main = mapM_ extraTask [47, 79, 191, 382, 379]</
<pre>L(47) = 8
Line 1,422:
Journal de théorie des nombres de Bordeaux, 6 no. 1 (1994), p. 21-38,'' [http://www.numdam.org/item?id=JTNB_1994__6_1_21_0].
<
binaryChain n | even n = n : binaryChain (n `div` 2)
| odd n = n : binaryChain (n - 1)
Line 1,441:
c1 `add` c2 = map (head c2 +) c1 ++ c2
log2 = floor . logBase 2 . fromIntegral</
<pre>λ> binaryChain 191
Line 1,457:
=={{header|Java}}==
{{trans|D}}
<
private static class Pair {
int f, s;
Line 1,510:
}
}
}</
{{out}}
<pre>N = 7
Line 1,562:
=={{header|Julia}}==
{{trans|Python}}
<
pos > minlen || seq[1] > n ? (minlen, 0) :
seq[1] == n ? (pos, 1) :
Line 1,587:
println("Number of minimum length Brauer chains: $nchains")
end
</
<pre>
N = 7
Line 1,631:
I've then extended the code to count the number of non-Brauer chains of the same minimum length - basically 'brute' force to generate all addition chains and then subtracted the number of Brauer ones - plus examples for both. For N <= 64 this adds little to the execution time but adds about 1 minute for N = 79 and I gave up waiting for N = 191! To deal with these glacial execution times, I've added code which enables you to suppress the non-Brauer generation for N above a specified figure.
<
var example: List<Int>? = null
Line 1,721:
println("Searching for Brauer chains up to a minimum length of 12:")
for (num in nums) findBrauer(num, 12, 79)
}</
{{out}}
Line 1,807:
=={{header|Lua}}==
{{trans|D}}
<
return a[i + 1]
end
Line 1,874:
end
main()</
{{out}}
<pre>N = 7
Line 1,927:
{{trans|Go}}
This is a translation of the second Go version.
<
const
Line 2,005:
if nonBrauerCount > 0:
echo "Non-Brauer example: ", nonBrauerExample.join(", ")
echo "\nTook ", now() - start</
{{out}}
Line 2,093:
=={{header|Perl}}==
{{trans|Raku}}
<
use feature 'say';
Line 2,201:
# 47, 79, 191, 382, 379, 379, 12509);
say "Searching for Brauer chains up to a minimum length of 12:";
for (@nums) { findBrauer $_, 12, 79 }</
{{out}}
<pre style="height:35ex">N = 7
Line 2,252:
Note the internal values of l(n) are [consistently] +1 compared to what the rest of the world says.
<!--<
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
Line 2,353:
<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;">"l(%d) = %d, Brauer:%d,%s Non-Brauer:%d,%s (%s, %d perms)\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">num</span><span style="color: #0000FF;">,</span><span style="color: #000000;">l</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">bc</span><span style="color: #0000FF;">,</span><span style="color: #000000;">bs</span><span style="color: #0000FF;">,</span><span style="color: #000000;">nbc</span><span style="color: #0000FF;">,</span><span style="color: #000000;">ns</span><span style="color: #0000FF;">,</span><span style="color: #000000;">e</span><span style="color: #0000FF;">,</span><span style="color: #000000;">tries</span><span style="color: #0000FF;">})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</
{{out}}
Line 2,391:
=={{header|Python}}==
{{trans|Java}}
<
return [n] + seq
Line 2,429:
nums = [7, 14, 21, 29, 32, 42, 64, 47, 79, 191, 382, 379]
for i in nums:
find_brauer(i)</
{{out}}
<pre>
Line 2,481:
====Faster method====
<
chain = [0]*n
in_chain = [False]*(n + 1)
Line 2,517:
for n in [7, 14, 21, 29, 32, 42, 64, 47, 79, 191, 382, 379]:
best, cnt = bauer(n)
print(f'L({n}) = {len(best) - 1}, count of minimum chain: {cnt}\ne.g.: {best}\n')</
{{out}}
<pre>
Line 2,539:
This implementation uses the [https://docs.racket-lang.org/rosette-guide/index.html Rosette] language in Racket. It is inefficient as it asks an SMT solver to enumerate every possible solutions. However, it is very straightforward to write, and in fact is quite efficient for computing <code>l(n)</code> and finding one example (solve n = 379 in ~3 seconds).
<
(define (basic-constraints xs n)
Line 2,595:
(for ([x (in-list '(191 382 379 12509))])
(compute/time x #:enumerate? #f))</
{{out}}
Line 2,685:
(formerly Perl 6)
{{trans|Kotlin}}
<
sub check-Sequence($pos, @seq, $n, $minLen --> List) {
Line 2,781:
say "Searching for Brauer chains up to a minimum length of 12:";
find-Brauer $_, 12, 79 for 7, 14, 21, 29, 32, 42, 64 #, 47, 79, 191, 382, 379, 379, 12509 # un-comment for extra-credit</
{{out}}
<pre>Searching for Brauer chains up to a minimum length of 12:
Line 2,832:
=={{header|Ruby}}==
{{trans|D}}
<
if pos > min_len or seq[0] > n then
return min_len, 0
Line 2,880:
end
main()</
{{out}}
<pre>
Line 2,933:
=={{header|Scala}}==
Following Scala implementation finds number of minimum length Brauer chains and corresponding length.
<
object chains{
Line 2,966:
}
}
</syntaxhighlight>
<pre>
N = 7
Line 3,023:
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<
Function Prepend(n As Integer, seq As List(Of Integer)) As List(Of Integer)
Line 3,081:
End Sub
End Module</
{{out}}
<pre>N = 7
Line 3,136:
Non-Brauer analysis limited to N = 191 in order to finish in a reasonable time - about 10.75 minutes on my machine.
<
var maxNonBrauer = 191
Line 3,223:
} else System.print("Non-Brauer analysis suppressed")
}
System.print("\nTook %(System.clock - start) seconds.")</
{{out}}
Line 3,312:
=={{header|zkl}}==
{{trans|EchoLisp}}
<
_minlg, _counts, _chains; // counters and results
Line 3,346:
}
}
}</
<
_minlg=(0).MAX;
chains(n,List(1),0);
Line 3,353:
.fmt(n,_minlg,_counts.xplode(),_chains.filter()));
}
T(7,14,21,29,32,42,64,47,79).apply2(task);</
{{out}}
<pre>
|