Generating unique, ordered Pythagorean triplets

前端 未结 19 1304
借酒劲吻你
借酒劲吻你 2020-11-29 16:57

This is a program I wrote to calculate Pythagorean triplets. When I run the program it prints each set of triplets twice because of the if statement. Is there any way I can

19条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-29 17:39

    Just checking, but I've been using the following code to make pythagorean triples. It's very fast (and I've tried some of the examples here, though I kind of learned them and wrote my own and came back and checked here (2 years ago)). I think this code correctly finds all pythagorean triples up to (name your limit) and fairly quickly too. I used C++ to make it.

    ullong is unsigned long long and I created a couple of functions to square and root my root function basically said if square root of given number (after making it whole number (integral)) squared not equal number give then return -1 because it is not rootable. _square and _root do as expected as of description above, I know of another way to optimize it but I haven't done nor tested that yet.

    generate(vector& triplist, ullong limit) {
    cout<<"Please wait as triples are being generated."< limit)
                break;
        }
    }
    
    timer = time(0) - timer;
    cout<<"Generated "<

    }

    Let me know what you all think. It generates all primitive and non-primitive triples according to the teacher I turned it in for. (she tested it up to 100 if I remember correctly).

    The results from the v4 supplied by a previous coder here are

    Below is a not-very-scientific set of timings (using Java under Eclipse on my older laptop with other stuff running...), where the "use x, y, z" was implemented by instantiating a Triple object with the three values and putting it in an ArrayList. (For these runs, N was set to 10,000, which produced 12,471 triples in each case.)

    Version 4: 46 sec. using square root: 134 sec. array and map: 400 sec.

    The results from mine is How many triples to generate: 10000

    Please wait as triples are being generated. Generated 12471 in 2 seconds.

    That is before I even start optimizing via the compiler. (I remember previously getting 10000 down to 0 seconds with tons of special options and stuff). My code also generates all the triples with 100,000 as the limit of how high side1,2,hyp can go in 3.2 minutes (I think the 1,000,000 limit takes an hour).

    I modified the code a bit and got the 10,000 limit down to 1 second (no optimizations). On top of that, with careful thinking, mine could be broken down into chunks and threaded upon given ranges (for example 100,000 divide into 4 equal chunks for 3 cpu's (1 extra to hopefully consume cpu time just in case) with ranges 1 to 25,000 (start at 1 and limit it to 25,000), 25,000 to 50,000 , 50,000 to 75,000, and 75,000 to end. I may do that and see if it speeds it up any (I will have threads premade and not include them in the actual amount of time to execute the triple function. I'd need a more precise timer and a way to concatenate the vectors. I think that if 1 3.4 GHZ cpu with 8 gb ram at it's disposal can do 10,000 as lim in 1 second then 3 cpus should do that in 1/3 a second (and I round to higher second as is atm).

提交回复
热议问题