Pythagorean triples: Difference between revisions
Content added Content deleted
(Added Elixir) |
|||
Line 795: | Line 795: | ||
There are 64741 triples, below 100000. Of which 7026 are primitives. |
There are 64741 triples, below 100000. Of which 7026 are primitives. |
||
There are 808950 triples, below 1000000. Of which 70229 are primitives. |
There are 808950 triples, below 1000000. Of which 70229 are primitives. |
||
</pre> |
|||
=={{header|Elixir}}== |
|||
{{trans|Ruby}} |
|||
<lang elixir>defmodule RC do |
|||
def count_triples(limit), do: count_triples(limit,3,4,5) |
|||
defp count_triples(limit, a, b, c) when limit<(a+b+c), do: {0,0} |
|||
defp count_triples(limit, a, b, c) do |
|||
{p1, t1} = count_triples(limit, a-2*b+2*c, 2*a-b+2*c, 2*a-2*b+3*c) |
|||
{p2, t2} = count_triples(limit, a+2*b+2*c, 2*a+b+2*c, 2*a+2*b+3*c) |
|||
{p3, t3} = count_triples(limit,-a+2*b+2*c,-2*a+b+2*c,-2*a+2*b+3*c) |
|||
{1+p1+p2+p3, div(limit, a+b+c)+t1+t2+t3} |
|||
end |
|||
end |
|||
list = for n <- 1..8, do: Enum.reduce(1..n, 1, fn(_,acc)->10*acc end) |
|||
Enum.each(list, fn n -> IO.inspect {n, RC.count_triples(n)} end)</lang> |
|||
{{out}} |
|||
<pre> |
|||
{10, {0, 0}} |
|||
{100, {7, 17}} |
|||
{1000, {70, 325}} |
|||
{10000, {703, 4858}} |
|||
{100000, {7026, 64741}} |
|||
{1000000, {70229, 808950}} |
|||
{10000000, {702309, 9706567}} |
|||
{100000000, {7023027, 113236940}} |
|||
</pre> |
</pre> |
||
Line 2,723: | Line 2,752: | ||
@limit = limit |
@limit = limit |
||
@total = 0 |
@total = 0 |
||
@ |
@primitives = 0 |
||
generate_triples(3, 4, 5) |
generate_triples(3, 4, 5) |
||
end |
end |
||
attr_reader :total, : |
attr_reader :total, :primitives |
||
private |
private |
||
Line 2,733: | Line 2,762: | ||
return if perim > @limit |
return if perim > @limit |
||
@ |
@primitives += 1 |
||
@total += @limit / perim |
@total += @limit / perim |
||
Line 2,745: | Line 2,774: | ||
while perim <= 100_000_000 |
while perim <= 100_000_000 |
||
c = PythagoranTriplesCounter.new perim |
c = PythagoranTriplesCounter.new perim |
||
p [perim, c.total, c. |
p [perim, c.total, c.primitives] |
||
perim *= 10 |
perim *= 10 |
||
end</lang> |
end</lang> |