Addition chains: Difference between revisions

m
syntax highlighting fixup automation
(→‎{{header|Haskell}}: added practical solution)
m (syntax highlighting fixup automation)
Line 26:
{{trans|Python}}
 
<langsyntaxhighlight lang=11l>F bauer(n)
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))</langsyntaxhighlight>
 
{{out}}
Line 105:
=={{header|C}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang=c>#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Line 300:
for (i = 0; i < 12; ++i) findBrauer(nums[i], 12, 79);
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 386:
=={{header|C sharp|C#}}==
{{trans|Java}}
<langsyntaxhighlight lang=csharp>using System;
 
namespace AdditionChains {
Line 433:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>N = 7
Line 486:
While this worked, something made it run extremely slow.
{{trans|D}}
<langsyntaxhighlight lang=cpp>#include <iostream>
#include <tuple>
#include <vector>
Line 531:
 
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>
Line 584:
=={{header|D}}==
{{trans|Scala}}
<langsyntaxhighlight lang=D>import std.stdio;
import std.typecons;
 
Line 622:
find_brauer(i);
}
}</langsyntaxhighlight>
{{out}}
<pre>N = 7
Line 673:
 
=={{header|EchoLisp}}==
<langsyntaxhighlight lang=scheme>
;; 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>
</lang>
{{out}}
<pre>
Line 743:
===Version 1===
{{trans|Kotlin}}
<langsyntaxhighlight lang=go>package main
 
import "fmt"
Line 896:
findBrauer(num, 12, 79)
}
}</langsyntaxhighlight>
 
{{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.
<langsyntaxhighlight lang=go>package main
 
import (
Line 1,100:
}
fmt.Printf("\nTook %s\n", time.Since(start))
}</langsyntaxhighlight>
 
{{out}}
Line 1,191:
=={{header|Groovy}}==
{{trans|Java}}
<langsyntaxhighlight lang=Groovy>class AdditionChains {
private static class Pair {
int f, s
Line 1,244:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>N = 7
Line 1,298:
Implementation using backtracking.
 
<langsyntaxhighlight lang=haskell>import Data.List (union)
 
-- search strategies
Line 1,320:
isBrauer [_] = True
isBrauer [_,_] = True
isBrauer (x:y:xs) = (x - y) `elem` (y:xs) && isBrauer (y:xs)</langsyntaxhighlight>
 
Usage examples
Line 1,340:
Tasks implementation
 
<langsyntaxhighlight lang=haskell>task :: Int -> IO()
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"</langsyntaxhighlight>
 
<pre>λ> mapM_ task [7,14,21,29,32,42,64]
Line 1,385:
For the extra task used compiled code, not GHCi.
 
<langsyntaxhighlight lang=haskell>extraTask :: Int -> IO()
extraTask n =
let ch = chains brauer n
Line 1,393:
putStrLn "Non-Brauer analysis suppressed\n"
 
main = mapM_ extraTask [47, 79, 191, 382, 379]</langsyntaxhighlight>
 
<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].
 
<langsyntaxhighlight lang=haskell>binaryChain 1 = [1]
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</langsyntaxhighlight>
 
<pre>λ> binaryChain 191
Line 1,457:
=={{header|Java}}==
{{trans|D}}
<langsyntaxhighlight lang=Java>public class AdditionChains {
private static class Pair {
int f, s;
Line 1,510:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>N = 7
Line 1,562:
=={{header|Julia}}==
{{trans|Python}}
<langsyntaxhighlight lang=julia>checksequence(pos, seq, n, minlen) =
pos > minlen || seq[1] > n ? (minlen, 0) :
seq[1] == n ? (pos, 1) :
Line 1,587:
println("Number of minimum length Brauer chains: $nchains")
end
</langsyntaxhighlight>{{out}}
<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.
<langsyntaxhighlight lang=scala>// version 1.1.51
 
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)
}</langsyntaxhighlight>
 
{{out}}
Line 1,807:
=={{header|Lua}}==
{{trans|D}}
<langsyntaxhighlight lang=lua>function index(a,i)
return a[i + 1]
end
Line 1,874:
end
 
main()</langsyntaxhighlight>
{{out}}
<pre>N = 7
Line 1,927:
{{trans|Go}}
This is a translation of the second Go version.
<langsyntaxhighlight lang=Nim>import times, strutils
 
const
Line 2,005:
if nonBrauerCount > 0:
echo "Non-Brauer example: ", nonBrauerExample.join(", ")
echo "\nTook ", now() - start</langsyntaxhighlight>
 
{{out}}
Line 2,093:
=={{header|Perl}}==
{{trans|Raku}}
<langsyntaxhighlight lang=perl>use strict;
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 }</langsyntaxhighlight>
{{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.
 
<!--<langsyntaxhighlight lang=Phix>(phixonline)-->
<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>
<!--</langsyntaxhighlight>-->
 
{{out}}
Line 2,391:
=={{header|Python}}==
{{trans|Java}}
<langsyntaxhighlight lang=python>def prepend(n, seq):
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)</langsyntaxhighlight>
{{out}}
<pre>
Line 2,481:
 
====Faster method====
<langsyntaxhighlight lang=python>def bauer(n):
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')</langsyntaxhighlight>
{{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).
 
<langsyntaxhighlight lang=racket>#lang rosette
 
(define (basic-constraints xs n)
Line 2,595:
 
(for ([x (in-list '(191 382 379 12509))])
(compute/time x #:enumerate? #f))</langsyntaxhighlight>
 
{{out}}
Line 2,685:
(formerly Perl 6)
{{trans|Kotlin}}
<langsyntaxhighlight lang=perl6>my @Example = ();
 
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</langsyntaxhighlight>
{{out}}
<pre>Searching for Brauer chains up to a minimum length of 12:
Line 2,832:
=={{header|Ruby}}==
{{trans|D}}
<langsyntaxhighlight lang=ruby>def check_seq(pos, seq, n, min_len)
if pos > min_len or seq[0] > n then
return min_len, 0
Line 2,880:
end
 
main()</langsyntaxhighlight>
{{out}}
<pre>
Line 2,933:
=={{header|Scala}}==
Following Scala implementation finds number of minimum length Brauer chains and corresponding length.
<langsyntaxhighlight lang=Scala>
object chains{
 
Line 2,966:
}
}
</syntaxhighlight>
</lang>
<pre>
N = 7
Line 3,023:
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<langsyntaxhighlight lang=vbnet>Module Module1
 
Function Prepend(n As Integer, seq As List(Of Integer)) As List(Of Integer)
Line 3,081:
End Sub
 
End Module</langsyntaxhighlight>
{{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.
<langsyntaxhighlight lang=ecmascript>var maxLen = 13
var maxNonBrauer = 191
 
Line 3,223:
} else System.print("Non-Brauer analysis suppressed")
}
System.print("\nTook %(System.clock - start) seconds.")</langsyntaxhighlight>
 
{{out}}
Line 3,312:
=={{header|zkl}}==
{{trans|EchoLisp}}
<langsyntaxhighlight lang=zkl>var exp2=(32).pump(List,(2).pow), // 2^n, n=0..31
_minlg, _counts, _chains; // counters and results
Line 3,346:
}
}
}</langsyntaxhighlight>
<langsyntaxhighlight lang=zkl>fcn task(n){
_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);</langsyntaxhighlight>
{{out}}
<pre>
10,327

edits