Skip to content

Instantly share code, notes, and snippets.

@martian0x80
Created October 26, 2022 17:36
Show Gist options
  • Save martian0x80/ba2a7acb97b8a399ef442f53c92c9f75 to your computer and use it in GitHub Desktop.
Save martian0x80/ba2a7acb97b8a399ef442f53c92c9f75 to your computer and use it in GitHub Desktop.
Berggrens Tree to generate primitive triples. Some inefficient way to generate millions of triples. :)
function primitive_triples(depth::Int64, max_sum::Int64 = 0, list::Bool = false)
#=
depth : tree depth
each depth adds 3^i new triples each depth.
total triples with depth 'd' : ∑_(i=0;d) 3^i
max_sum : maximum perimeter of the right triangle formed by the triples, breaks if the limit (max_sum) is reached, but includes the last 3 triples one of which exceed the limit.
0 is the default value, to ignore any restrictions.
list : return a flat array of the triples
=#
node_0 = Vector{Int64}([3; 4; 5])
nodes = Dict{Int64, Vector{Vector{Int64}}}([0 => [node_0]])
list && (ptriples = Vector{Vector{Int64}}([node_0]))
Barning_matrix_1 = Matrix{Int8}([[1 -2 2]; [2 -1 2]; [2 -2 3]])
Barning_matrix_2 = Matrix{Int8}([[1 2 2]; [2 1 2]; [2 2 3]])
Barning_matrix_3 = Matrix{Int8}([[-1 2 2]; [-2 1 2]; [-2 2 3]])
nodesofar = 0
count = 0
while nodesofar < depth
triples = Vector{Vector{Int64}}()
for i in nodes[nodesofar]
vec1 = Barning_matrix_1*i
vec2 = Barning_matrix_2*i
vec3 = Barning_matrix_3*i
push!(triples, vec1, vec2, vec3)
list && push!(ptriples, vec1, vec2, vec3)
if max_sum > 0
if sum(vec1) ≥ max_sum || sum(vec2) ≥ max_sum || sum(vec2) ≥ max_sum
nodes[nodesofar + 1] = triples
list ? (return ptriples) : (return nodes, count + 1)
end
end
count += 3
end
nodes[nodesofar + 1] = triples
nodesofar += 1
end
list ? (return ptriples) : (return nodes, count + 1)
end
@time println(primitive_triples(5, 0, false))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment