Special pythagorean triplet: Difference between revisions
Content added Content deleted
m (→{{header|Ring}}: minor deletes, whitespace) |
(added FreeBASIC) |
||
Line 450: | Line 450: | ||
Real: 00:00:03.704 |
Real: 00:00:03.704 |
||
</pre> |
</pre> |
||
=={{header|FreeBASIC}}== |
|||
<lang freebasic>' version 06-10-2021 |
|||
' compile with: fbc -s console |
|||
' ------------------------------------------------------------- |
|||
' Brute force |
|||
Dim As UInteger a, b, c |
|||
Dim As Double t1 = Timer |
|||
Print "Brute force" : Print |
|||
For a = 1 To 1000 |
|||
For b = a+1 To 1000 |
|||
For c = b+1 To 1000 |
|||
If (a*a+b*b) = (c*c) Then |
|||
If a+b+c = 1000 Then |
|||
Print a,b,c |
|||
End If |
|||
End If |
|||
Next |
|||
Next |
|||
Next |
|||
Print |
|||
Print Using "runtime: ###.## msec.";(Timer-t1)*1000 |
|||
Print String(60,"-") : Print |
|||
' ------------------------------------------------------------- |
|||
' limit for a = 1000\3 and b = 1000\2, c = 1000 - a - c |
|||
Print "Set limits for a and b, c = 1000 - a - b" : Print |
|||
t1 = Timer |
|||
For a = 3 To 333 |
|||
For b = a+1 To 500 |
|||
For c= b+1 To (1000-a-b) |
|||
If a+b+c = 1000 Then |
|||
If (a*a+b*b) = (c*c) Then |
|||
Print a,b,c |
|||
Exit For,For,For |
|||
End If |
|||
End If |
|||
Next |
|||
Next |
|||
Next |
|||
Print |
|||
Print Using "runtime: ###.## msec.";(Timer-t1)*1000 |
|||
Print String(60,"-") : Print |
|||
' ------------------------------------------------------------- |
|||
' primative pythagoras triples |
|||
' m and n are positive integer and odd |
|||
' m > n, m start at 3 |
|||
' if n > 1 then GCD(m,n) must be 1 |
|||
' a = m * n |
|||
' b = (m ^ 2 - n ^ 2) \ 2 |
|||
' c = (m ^ 2 + n ^ 2) \ 2 |
|||
' swap a and b if a > b |
|||
Function gcd(x As UInteger, y As UInteger) As UInteger |
|||
While y |
|||
Dim As UInteger t = y |
|||
y = x Mod y |
|||
x = t |
|||
Wend |
|||
Return x |
|||
End Function |
|||
Function check(n As UInteger) As Boolean |
|||
If n And 1 = 0 Then Return FALSE |
|||
For i As UInteger = 3 To Sqr(n) |
|||
If n Mod i = 0 Then Return TRUE |
|||
Next |
|||
Return FALSE |
|||
End Function |
|||
Dim As UInteger m, n, temp |
|||
Print "Using primative pythagoras triples" : Print |
|||
t1 = Timer |
|||
For m = 3 To 201 Step 2 |
|||
For n = 1 To m Step 2 |
|||
If n > 1 And (gcd(m, n) <> 1) Then |
|||
Continue For |
|||
End If |
|||
a = m * n |
|||
b = (m * m - n * n) \ 2 |
|||
c = b + n * n |
|||
If a > b Then Swap a, b |
|||
temp = a + b + c |
|||
If 1000 Mod temp = 0 Then |
|||
' temp must be odd and split in two number (no prime) |
|||
If check(temp) Then |
|||
temp = 1000 \ temp |
|||
a = a * temp |
|||
b = b * temp |
|||
c = c * temp |
|||
Print a,b,c |
|||
Exit For, For |
|||
End If |
|||
End If |
|||
Next |
|||
Next |
|||
Print |
|||
Print Using "runtime: ###.## msec.";(Timer-t1)*1000 |
|||
Print String(60,"-") : Print |
|||
' empty keyboard buffer |
|||
While InKey <> "" : Wend |
|||
Print : Print "hit any key to end program" |
|||
Sleep |
|||
End</lang> |
|||
{{out}} |
|||
<pre>Brute force |
|||
200 375 425 |
|||
runtime: 555.19 msec. |
|||
------------------------------------------------------------ |
|||
Set limits for a and b, c = 1000 - a - b |
|||
200 375 425 |
|||
runtime: 68.91 msec. |
|||
------------------------------------------------------------ |
|||
Using primative pythagoras triples |
|||
200 375 425 |
|||
runtime: 2.49 msec. |
|||
------------------------------------------------------------</pre> |
|||
=={{header|Go}}== |
=={{header|Go}}== |