Skip to content

my-LinkedIn/pythagorean-triples

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 

Repository files navigation

PYTHAGOREAN TRIPLES

Basic Implementation

import std.stdio;
import std.range;
import std.algorithm;

void main()
{
    const N = 20;

    alias Triples = int[][];
    Triples triples;

    foreach (a; 1..N+1)
        foreach (b; a..N+1)
            foreach (c; b..N+1)
                if (a^^2 + b^^2 == c^^2) triples ~= [a, b, c];

    triples.each!(triple => triple.writeln);
}

Basic Implementation but using Tuple

import std.stdio;
import std.array;
import std.range;
import std.typecons;
import std.algorithm;

void main()
{
    const N = 20;
    
    alias Triples = Tuple!(int, int, int)[]; 
    Triples triples;

    foreach (a; 1..N+1)
        foreach (b; a..N+1)
            foreach (c; b..N+1)
                if (a^^2 + b^^2 == c^^2) triples ~= tuple(a, b, c);
    
    triples.each!(triple => triple.array.writeln);
}

Cartesian Product Implementation

import std.stdio;
import std.array;
import std.range;
import std.algorithm;
import std.algorithm.iteration;
import std.typecons;

void main()
{
    const N = 20;
    
    auto ts = cartesianProduct(iota(1,N+1), iota(1,N+1), iota(1,N+1))
        .filter!(t => t[0]^^2 + t[1]^^2 == t[2]^^2)
        .map!(t => tuple(t[0], t[1], t[2]));

    ts.map!array.each!(triple => triple.writeln);
}

Special Pythagorean Triplet - Project Euler problem 9 solution

You can find the Problem statement here.

import std.stdio;
import std.range;

void main()
{
    foreach (a; iota(0, 333))
        foreach (b; iota(a + 1, 500))
            {
              auto c = 1000-(a+b);
              
              if (a*a + b*b == c*c) {
                  writeln(a, " * ", b, " * ", c, " = ", a * b * c);
                  return;
              }
            }
   
    writeln("No Triple found!");
}

About

Simple Pythagorean Triples D programs

Topics

Resources

Stars

Watchers

Forks