Here's a simple brute-force algorithm to generate Pythagorean triplets.
Written in
Flaming Thunder
, works in FT Notepad and the free version.
# Pythagorean Triplets! # a² + b² = c², where a, b, and c are all integers. # Here's a brute-force algorithm to find a lot! # 23 commands, 4 labels, tons of comments! # First, we need the following conditions: Set a to 0. Set b to 0. Set c to 0. Set BiggestNumber to 1000. ######################################### ## WARNING: This is an n² algorithm! ## ## Picking an end number 10 times the ## ## current can take 100 times as long! ## ######################################### # And let's go ahead and define a file to write them into. Set Path to "triplets.txt". Remove Path. # Now, we're going to set up a bit of a fancy loop here. # The goal is to not have any duplicates. # At the same time, we also only want to deal with integers. # (For precision reasons) # For each a, we'll loop through all b satisfying: # a² + b² <= BiggestNumber². # Lots of jumping around here. OuterLoop: Set a to a + 1. Set b to a + 1. Set c to a + 2. # a is always the smallest number of the three, # b and c are set to skip impossible combos # as well as to avoid associative duplicates. If a > BiggestNumber then go to Done. # Exit condition. InnerLoop: If c > BiggestNumber then go to OuterLoop. # No numbers bigger than BiggestNumber! Set Leftside to a*a + b*b. Set Rightside to c*c. # Leftside = a² + b², Rightside = c². If Leftside = Rightside then go to Success. # If they're equal, we found a triplet! If Leftside < Rightside then ( Set b to b + 1. Go to InnerLoop. ). # If a² + b² < c², we make b a little bigger. If Leftside > Rightside then ( Set c to c + 1. Go to InnerLoop. ). # If a² + b² > c², we make c a little bigger. Success: Writeline a,"² + ",b,"² = ",c,"²" to Path. # If you've set BiggestNumber really big, you may feel that # your computer isn't working very fast. If you'd like to # see it working, uncomment the next line: # Writeline a,"^2 + ",b,"^2 = ",c,"^2". # (Just delete the "#" in front of the Writeline command.) Set b to b + 1. Go to InnerLoop. # After we've found one, we go back to the search! Done: # That's it. Just open whatever you set the path to. # (In this example - triplets.txt)