WeixiangYan/CodeTransOcean huggingface dataset:
Task: Sum multiples of 3 and 5







Python: def sum35a(n):
    'Direct count'
    
    return sum(x for x in range(n) if x%3==0 or x%5==0)

def sum35b(n): 
    "Count all the 3's; all the 5's; minus double-counted 3*5's"
    
    return sum(range(3, n, 3)) + sum(range(5, n, 5)) - sum(range(15, n, 15))
    
def sum35c(n):
    'Sum the arithmetic progressions: sum3 + sum5 - sum15'
    consts = (3, 5, 15)
    
    divs = [(n-1) // c for c in consts]
    sums = [d*c*(1+d)/2 for d,c in zip(divs, consts)]
    return sums[0] + sums[1] - sums[2]


for n in range(1001):
    sa, sb, sc = sum35a(n), sum35b(n), sum35c(n)
    assert sa == sb == sc  

print('For n = %7i -> %i\n' % (n, sc))


for p in range(7):
    print('For n = %7i -> %i' % (10**p, sum35c(10**p)))


p = 20
print('\nFor n = %20i -> %i' % (10**p, sum35c(10**p)))




Fortran:       INTEGER*8 FUNCTION SUMI(N)	
Calculates as per the young Gauss: N*(N + 1)/2 = 1 + 2 + 3 + ... + N.
       INTEGER*8 N	
        IF (MOD(N,2).EQ.0) THEN	
          SUMI = N/2*(N + 1)		
         ELSE			
          SUMI = (N + 1)/2*N		
        END IF			
      END FUNCTION SUMI		

      INTEGER*8 FUNCTION SUMF(N,F)	
       INTEGER*8 N,F		
       INTEGER*8 L		
       INTEGER*8 SUMI		
        L = (N - 1)/F		
        SUMF = F*SUMI(L)	
      END FUNCTION SUMF		

      INTEGER*8 FUNCTION SUMBFI(N)	
       INTEGER*8 N	
       INTEGER*8 I,S	
        S = 0		
        DO I = 3,N - 1	
          IF (MOD(I,3).EQ.0 .OR. MOD(I,5).EQ.0) S = S + I	
        END DO		
        SUMBFI = S		
      END FUNCTION SUMBFI	

      INTEGER*8 SUMF,SUMBFI	
      INTEGER*8 N	
      WRITE (6,*) "Sum multiples of 3 and 5 up to N"
   10 WRITE (6,11)		
   11 FORMAT ("Specify N: ",$)	
      READ (5,*) N		
      IF (N.LE.0) STOP		
      WRITE (6,*) "By Gauss:",SUMF(N,3) + SUMF(N,5) - SUMF(N,15)
      WRITE (6,*) "BFI sum :",SUMBFI(N)		
      GO TO 10			
      END
Task: Sum multiples of 3 and 5








VB: Private Function SumMult3and5VBScript(n As Double) As Double
Dim i As Double
    For i = 1 To n - 1
        If i Mod 3 = 0 Or i Mod 5 = 0 Then
            SumMult3and5VBScript = SumMult3and5VBScript + i
        End If
    Next
End Function



Fortran:       INTEGER*8 FUNCTION SUMI(N)	
Calculates as per the young Gauss: N*(N + 1)/2 = 1 + 2 + 3 + ... + N.
       INTEGER*8 N	
        IF (MOD(N,2).EQ.0) THEN	
          SUMI = N/2*(N + 1)		
         ELSE			
          SUMI = (N + 1)/2*N		
        END IF			
      END FUNCTION SUMI		

      INTEGER*8 FUNCTION SUMF(N,F)	
       INTEGER*8 N,F		
       INTEGER*8 L		
       INTEGER*8 SUMI		
        L = (N - 1)/F		
        SUMF = F*SUMI(L)	
      END FUNCTION SUMF		

      INTEGER*8 FUNCTION SUMBFI(N)	
       INTEGER*8 N	
       INTEGER*8 I,S	
        S = 0		
        DO I = 3,N - 1	
          IF (MOD(I,3).EQ.0 .OR. MOD(I,5).EQ.0) S = S + I	
        END DO		
        SUMBFI = S		
      END FUNCTION SUMBFI	

      INTEGER*8 SUMF,SUMBFI	
      INTEGER*8 N	
      WRITE (6,*) "Sum multiples of 3 and 5 up to N"
   10 WRITE (6,11)		
   11 FORMAT ("Specify N: ",$)	
      READ (5,*) N		
      IF (N.LE.0) STOP		
      WRITE (6,*) "By Gauss:",SUMF(N,3) + SUMF(N,5) - SUMF(N,15)
      WRITE (6,*) "BFI sum :",SUMBFI(N)		
      GO TO 10			
      END
Task: Sum multiples of 3 and 5






PHP: $max = 1000;
$sum = 0;
for ($i = 1 ; $i < $max ; $i++) {
    if (($i % 3 == 0) or ($i % 5 == 0)) {
        $sum += $i;
    }
}
echo $sum, PHP_EOL;





Fortran:       INTEGER*8 FUNCTION SUMI(N)	
Calculates as per the young Gauss: N*(N + 1)/2 = 1 + 2 + 3 + ... + N.
       INTEGER*8 N	
        IF (MOD(N,2).EQ.0) THEN	
          SUMI = N/2*(N + 1)		
         ELSE			
          SUMI = (N + 1)/2*N		
        END IF			
      END FUNCTION SUMI		

      INTEGER*8 FUNCTION SUMF(N,F)	
       INTEGER*8 N,F		
       INTEGER*8 L		
       INTEGER*8 SUMI		
        L = (N - 1)/F		
        SUMF = F*SUMI(L)	
      END FUNCTION SUMF		

      INTEGER*8 FUNCTION SUMBFI(N)	
       INTEGER*8 N	
       INTEGER*8 I,S	
        S = 0		
        DO I = 3,N - 1	
          IF (MOD(I,3).EQ.0 .OR. MOD(I,5).EQ.0) S = S + I	
        END DO		
        SUMBFI = S		
      END FUNCTION SUMBFI	

      INTEGER*8 SUMF,SUMBFI	
      INTEGER*8 N	
      WRITE (6,*) "Sum multiples of 3 and 5 up to N"
   10 WRITE (6,11)		
   11 FORMAT ("Specify N: ",$)	
      READ (5,*) N		
      IF (N.LE.0) STOP		
      WRITE (6,*) "By Gauss:",SUMF(N,3) + SUMF(N,5) - SUMF(N,15)
      WRITE (6,*) "BFI sum :",SUMBFI(N)		
      GO TO 10			
      END
Task: Sum multiples of 3 and 5

C: #include <stdio.h>
#include <stdlib.h>

unsigned long long sum35(unsigned long long limit)
{
    unsigned long long sum = 0;
    for (unsigned long long i = 0; i < limit; i++)
        if (!(i % 3) || !(i % 5))
            sum += i;
    return sum;
}

int main(int argc, char **argv)
{
    unsigned long long limit;

    if (argc == 2)
        limit = strtoull(argv[1], NULL, 10);
    else
        limit = 1000;

    printf("%lld\n", sum35(limit));
    return 0;
}









COBOL: Identification division.
Program-id. three-five-sum.

Data division.
Working-storage section.
01 ws-the-limit  pic 9(18) value 1000.
01 ws-the-number pic 9(18).
01 ws-the-sum    pic 9(18).
01 ws-sum-out    pic z(18).

Procedure division.
Main-program.
    Perform Do-sum
        varying ws-the-number from 1 by 1 
        until ws-the-number = ws-the-limit.
    Move ws-the-sum to ws-sum-out.
    Display "Sum = " ws-sum-out.
    End-run.

Do-sum.
    If function mod(ws-the-number, 3) = zero
       or function mod(ws-the-number, 5) = zero
       then add ws-the-number to ws-the-sum.
Task: Sum multiples of 3 and 5



C#: using System;
using System.Collections.Generic;
using System.Numerics;

namespace RosettaCode
{
    class Program
    {
        static void Main()
        {
            List<BigInteger> candidates = new List<BigInteger>(new BigInteger[] { 1000, 100000, 10000000, 10000000000, 1000000000000000 });
            candidates.Add(BigInteger.Parse("100000000000000000000"));

            foreach (BigInteger candidate in candidates)
            {
                BigInteger c = candidate - 1;
                BigInteger answer3 = GetSumOfNumbersDivisibleByN(c, 3);
                BigInteger answer5 = GetSumOfNumbersDivisibleByN(c, 5);
                BigInteger answer15 = GetSumOfNumbersDivisibleByN(c, 15);

                Console.WriteLine("The sum of numbers divisible by 3 or 5 between 1 and {0} is {1}", c, answer3 + answer5 - answer15);
            }

            Console.ReadKey(true);
        }

        private static BigInteger GetSumOfNumbersDivisibleByN(BigInteger candidate, uint n)
        {
            BigInteger largest = candidate;
            while (largest % n > 0)
                largest--;
            BigInteger totalCount = (largest / n);
            BigInteger pairCount = totalCount / 2;
            bool unpairedNumberOnFoldLine = (totalCount % 2 == 1);
            BigInteger pairSum = largest + n;
            return pairCount * pairSum + (unpairedNumberOnFoldLine ? pairSum / 2 : 0);
        }

    }
}







COBOL: Identification division.
Program-id. three-five-sum.

Data division.
Working-storage section.
01 ws-the-limit  pic 9(18) value 1000.
01 ws-the-number pic 9(18).
01 ws-the-sum    pic 9(18).
01 ws-sum-out    pic z(18).

Procedure division.
Main-program.
    Perform Do-sum
        varying ws-the-number from 1 by 1 
        until ws-the-number = ws-the-limit.
    Move ws-the-sum to ws-sum-out.
    Display "Sum = " ws-sum-out.
    End-run.

Do-sum.
    If function mod(ws-the-number, 3) = zero
       or function mod(ws-the-number, 5) = zero
       then add ws-the-number to ws-the-sum.
Task: Sum multiples of 3 and 5


C++: #include <iostream>


typedef unsigned long long bigInt;

using namespace std;

class m35
{
public:
    void doIt( bigInt i )
    {
	bigInt sum = 0;
	for( bigInt a = 1; a < i; a++ )
	    if( !( a % 3 ) || !( a % 5 ) ) sum += a;

	cout << "Sum is " << sum << " for n = " << i << endl << endl;
    }
	
    
    void doIt_b( bigInt i )
    {
	bigInt sum = 0;
	for( bigInt a = 0; a < 28; a++ )
	{
	    if( !( a % 3 ) || !( a % 5 ) )
	    {
		sum += a;
		for( bigInt s = 30; s < i; s += 30 )
		    if( a + s < i ) sum += ( a + s );

	    }
	}
	cout << "Sum is " << sum << " for n = " << i << endl << endl;
    }
};

int main( int argc, char* argv[] )
{
    m35 m; m.doIt( 1000 );
    return system( "pause" );
}








COBOL: Identification division.
Program-id. three-five-sum.

Data division.
Working-storage section.
01 ws-the-limit  pic 9(18) value 1000.
01 ws-the-number pic 9(18).
01 ws-the-sum    pic 9(18).
01 ws-sum-out    pic z(18).

Procedure division.
Main-program.
    Perform Do-sum
        varying ws-the-number from 1 by 1 
        until ws-the-number = ws-the-limit.
    Move ws-the-sum to ws-sum-out.
    Display "Sum = " ws-sum-out.
    End-run.

Do-sum.
    If function mod(ws-the-number, 3) = zero
       or function mod(ws-the-number, 5) = zero
       then add ws-the-number to ws-the-sum.
Task: Sum multiples of 3 and 5





Java: class SumMultiples {
	public static long getSum(long n) {
		long sum = 0;
		for (int i = 3; i < n; i++) {
			if (i % 3 == 0 || i % 5 == 0) sum += i;
		}
		return sum;
	}
	public static void main(String[] args) {
		System.out.println(getSum(1000));
	}
}





COBOL: Identification division.
Program-id. three-five-sum.

Data division.
Working-storage section.
01 ws-the-limit  pic 9(18) value 1000.
01 ws-the-number pic 9(18).
01 ws-the-sum    pic 9(18).
01 ws-sum-out    pic z(18).

Procedure division.
Main-program.
    Perform Do-sum
        varying ws-the-number from 1 by 1 
        until ws-the-number = ws-the-limit.
    Move ws-the-sum to ws-sum-out.
    Display "Sum = " ws-sum-out.
    End-run.

Do-sum.
    If function mod(ws-the-number, 3) = zero
       or function mod(ws-the-number, 5) = zero
       then add ws-the-number to ws-the-sum.
Task: Sum multiples of 3 and 5







Python: def sum35a(n):
    'Direct count'
    
    return sum(x for x in range(n) if x%3==0 or x%5==0)

def sum35b(n): 
    "Count all the 3's; all the 5's; minus double-counted 3*5's"
    
    return sum(range(3, n, 3)) + sum(range(5, n, 5)) - sum(range(15, n, 15))
    
def sum35c(n):
    'Sum the arithmetic progressions: sum3 + sum5 - sum15'
    consts = (3, 5, 15)
    
    divs = [(n-1) // c for c in consts]
    sums = [d*c*(1+d)/2 for d,c in zip(divs, consts)]
    return sums[0] + sums[1] - sums[2]


for n in range(1001):
    sa, sb, sc = sum35a(n), sum35b(n), sum35c(n)
    assert sa == sb == sc  

print('For n = %7i -> %i\n' % (n, sc))


for p in range(7):
    print('For n = %7i -> %i' % (10**p, sum35c(10**p)))


p = 20
print('\nFor n = %20i -> %i' % (10**p, sum35c(10**p)))



COBOL: Identification division.
Program-id. three-five-sum.

Data division.
Working-storage section.
01 ws-the-limit  pic 9(18) value 1000.
01 ws-the-number pic 9(18).
01 ws-the-sum    pic 9(18).
01 ws-sum-out    pic z(18).

Procedure division.
Main-program.
    Perform Do-sum
        varying ws-the-number from 1 by 1 
        until ws-the-number = ws-the-limit.
    Move ws-the-sum to ws-sum-out.
    Display "Sum = " ws-sum-out.
    End-run.

Do-sum.
    If function mod(ws-the-number, 3) = zero
       or function mod(ws-the-number, 5) = zero
       then add ws-the-number to ws-the-sum.
Task: Sum multiples of 3 and 5








VB: Private Function SumMult3and5VBScript(n As Double) As Double
Dim i As Double
    For i = 1 To n - 1
        If i Mod 3 = 0 Or i Mod 5 = 0 Then
            SumMult3and5VBScript = SumMult3and5VBScript + i
        End If
    Next
End Function


COBOL: Identification division.
Program-id. three-five-sum.

Data division.
Working-storage section.
01 ws-the-limit  pic 9(18) value 1000.
01 ws-the-number pic 9(18).
01 ws-the-sum    pic 9(18).
01 ws-sum-out    pic z(18).

Procedure division.
Main-program.
    Perform Do-sum
        varying ws-the-number from 1 by 1 
        until ws-the-number = ws-the-limit.
    Move ws-the-sum to ws-sum-out.
    Display "Sum = " ws-sum-out.
    End-run.

Do-sum.
    If function mod(ws-the-number, 3) = zero
       or function mod(ws-the-number, 5) = zero
       then add ws-the-number to ws-the-sum.
Task: Sum multiples of 3 and 5




Go: package main

import "fmt"

func main() {
    fmt.Println(s35(1000))
}

func s35(n int) int {
    n--
    threes := n / 3
    fives := n / 5
    fifteen := n / 15

    threes = 3 * threes * (threes + 1)
    fives = 5 * fives * (fives + 1)
    fifteen = 15 * fifteen * (fifteen + 1)

    n = (threes + fives - fifteen) / 2
	
    return n
}






COBOL: Identification division.
Program-id. three-five-sum.

Data division.
Working-storage section.
01 ws-the-limit  pic 9(18) value 1000.
01 ws-the-number pic 9(18).
01 ws-the-sum    pic 9(18).
01 ws-sum-out    pic z(18).

Procedure division.
Main-program.
    Perform Do-sum
        varying ws-the-number from 1 by 1 
        until ws-the-number = ws-the-limit.
    Move ws-the-sum to ws-sum-out.
    Display "Sum = " ws-sum-out.
    End-run.

Do-sum.
    If function mod(ws-the-number, 3) = zero
       or function mod(ws-the-number, 5) = zero
       then add ws-the-number to ws-the-sum.
Task: Sum multiples of 3 and 5






PHP: $max = 1000;
$sum = 0;
for ($i = 1 ; $i < $max ; $i++) {
    if (($i % 3 == 0) or ($i % 5 == 0)) {
        $sum += $i;
    }
}
echo $sum, PHP_EOL;
Task: Sum multiples of 3 and 5






PHP: $max = 1000;
$sum = 0;
for ($i = 1 ; $i < $max ; $i++) {
    if (($i % 3 == 0) or ($i % 5 == 0)) {
        $sum += $i;
    }
}
echo $sum, PHP_EOL;






Ada: with Ada.Text_IO;

procedure Sum_Multiples is

   type Natural is range 0 .. 2**63 - 1;

   function Sum_3_5 (Limit : in Natural) return Natural is
      Sum : Natural := 0;
   begin
      for N in 1 .. Limit - 1 loop
         if N mod 3 = 0 or else N mod 5 = 0 then
            Sum := Sum + N;
         end if;
      end loop;
      return Sum;
   end Sum_3_5;

begin
   Ada.Text_IO.Put_Line ("n=1000: " & Sum_3_5 (1000)'Image);
   Ada.Text_IO.Put_Line ("n=5e9 : " & Sum_3_5 (5e9)'Image);
end Sum_Multiples;
Task: Sum multiples of 3 and 5






PHP: $max = 1000;
$sum = 0;
for ($i = 1 ; $i < $max ; $i++) {
    if (($i % 3 == 0) or ($i % 5 == 0)) {
        $sum += $i;
    }
}
echo $sum, PHP_EOL;




COBOL: Identification division.
Program-id. three-five-sum.

Data division.
Working-storage section.
01 ws-the-limit  pic 9(18) value 1000.
01 ws-the-number pic 9(18).
01 ws-the-sum    pic 9(18).
01 ws-sum-out    pic z(18).

Procedure division.
Main-program.
    Perform Do-sum
        varying ws-the-number from 1 by 1 
        until ws-the-number = ws-the-limit.
    Move ws-the-sum to ws-sum-out.
    Display "Sum = " ws-sum-out.
    End-run.

Do-sum.
    If function mod(ws-the-number, 3) = zero
       or function mod(ws-the-number, 5) = zero
       then add ws-the-number to ws-the-sum.
Task: Pascal's triangle



C#: using System;

namespace RosettaCode {

    class PascalsTriangle {

        public static void CreateTriangle(int n) {
            if (n > 0) {
                for (int i = 0; i < n; i++) {
                    int c = 1;
                    Console.Write(" ".PadLeft(2 * (n - 1 - i)));
                    for (int k = 0; k <= i; k++) {
                        Console.Write("{0}", c.ToString().PadLeft(3));
                        c = c * (i - k) / (k + 1);
                    }
                    Console.WriteLine();
                }
            }
        }

        public static void Main() {
            CreateTriangle(8);
        }
    }
}









Ada: package Pascal is
   
   type Row is array (Natural range <>) of Natural;
   
   function Length(R: Row) return Positive;
   
   function First_Row(Max_Length: Positive) return Row;
   
   function Next_Row(R: Row) return Row;
   
end Pascal;
Task: Pascal's triangle

C: #include <stdio.h>

void pascaltriangle(unsigned int n)
{
  unsigned int c, i, j, k;

  for(i=0; i < n; i++) {
    c = 1;
    for(j=1; j <= 2*(n-1-i); j++) printf(" ");
    for(k=0; k <= i; k++) {
      printf("%3d ", c);
      c = c * (i-k)/(k+1);
    }
    printf("\n");
  }
}

int main()
{
  pascaltriangle(8);
  return 0;
}











Ada: package Pascal is
   
   type Row is array (Natural range <>) of Natural;
   
   function Length(R: Row) return Positive;
   
   function First_Row(Max_Length: Positive) return Row;
   
   function Next_Row(R: Row) return Row;
   
end Pascal;
Task: Pascal's triangle


C++: #include <iostream>
#include <algorithm>
#include<cstdio>
using namespace std;
void Pascal_Triangle(int size) {

	int a[100][100];
	int i, j;

	
	for (i = 1; i <= size; i++) {
		a[i][1] = a[1][i] = 1;
	}
	
	
	for (i = 2; i <= size; i++) {
		for (j = 2; j <= size - i; j++) {
			if (a[i - 1][j] == 0 || a[i][j - 1] == 0) {
				break;
			}
			a[i][j] = a[i - 1][j] + a[i][j - 1];
		}
	}

	
	
	
	
	int row,space;
	for (i = 1; i < size; i++) {
		space=row=i;
		j=1;
		
		while(space<=size+(size-i)+1){
			 cout<<" ";
			 space++;
		 }
		
		while(j<=i){
			if (a[row][j] == 0){
				   break;
			   }
			
			if(j==1){
				printf("%d",a[row--][j++]);
			}
			else
				printf("%6d",a[row--][j++]);
		}
			cout<<"\n\n";
	}
	
}

int main()
{
	
	
	int size;
	cin>>size;
	Pascal_Triangle(size);
}

}










Ada: package Pascal is
   
   type Row is array (Natural range <>) of Natural;
   
   function Length(R: Row) return Positive;
   
   function First_Row(Max_Length: Positive) return Row;
   
   function Next_Row(R: Row) return Row;
   
end Pascal;
Task: Pascal's triangle




Go: package main

import "fmt"

func printTriangle(n int) {
    
    if n <= 0 {
        return
    }
    fmt.Println(1)
    if n == 1 {
        return
    }
    
    a := make([]int, (n+1)/2)
    a[0] = 1
    for row, middle := 1, 0; row < n; row++ {
        
        even := row&1 == 0
        if even {
            a[middle+1] = a[middle] * 2
        }
        for i := middle; i > 0; i-- {
            a[i] += a[i-1]
        }
        
        for i := 0; i <= middle; i++ {
            fmt.Print(a[i], " ")
        }
        if even {
            middle++
        }
        for i := middle; i >= 0; i-- {
            fmt.Print(a[i], " ")
        }
        fmt.Println("")
    }
}

func main() {
    printTriangle(4)
}








Ada: package Pascal is
   
   type Row is array (Natural range <>) of Natural;
   
   function Length(R: Row) return Positive;
   
   function First_Row(Max_Length: Positive) return Row;
   
   function Next_Row(R: Row) return Row;
   
end Pascal;
Task: Pascal's triangle





Java: import java.util.ArrayList;
...
public static void genPyrN(int rows){
	if(rows < 0) return;
	
	ArrayList<Integer> last = new ArrayList<Integer>();
	last.add(1);
	System.out.println(last);
	for(int i= 1;i <= rows;++i){
		
		ArrayList<Integer> thisRow= new ArrayList<Integer>();
		thisRow.add(last.get(0)); 
		for(int j= 1;j < i;++j){
			
			thisRow.add(last.get(j - 1) + last.get(j));
		}
		thisRow.add(last.get(0)); 
		last= thisRow;
		System.out.println(thisRow);
	}
}







Ada: package Pascal is
   
   type Row is array (Natural range <>) of Natural;
   
   function Length(R: Row) return Positive;
   
   function First_Row(Max_Length: Positive) return Row;
   
   function Next_Row(R: Row) return Row;
   
end Pascal;
Task: Pascal's triangle







Python: def pascal(n):
   
   row = [1]
   k = [0]
   for x in range(max(n,0)):
      print row
      row=[l+r for l,r in zip(row+k,k+row)]
   return n>=1





Ada: package Pascal is
   
   type Row is array (Natural range <>) of Natural;
   
   function Length(R: Row) return Positive;
   
   function First_Row(Max_Length: Positive) return Row;
   
   function Next_Row(R: Row) return Row;
   
end Pascal;
Task: Pascal's triangle








VB: Option Base 1
Private Sub pascal_triangle(n As Integer)
    Dim odd() As String
    Dim eve() As String
    ReDim odd(1)
    ReDim eve(2)
    odd(1) = "  1"
    For i = 1 To n
        If i Mod 2 = 1 Then
            Debug.Print String$(2 * n - 2 * i, " ") & Join(odd, " ")
            eve(1) = "  1"
            ReDim Preserve eve(i + 1)
            For j = 2 To i
                eve(j) = Format(CStr(Val(odd(j - 1)) + Val(odd(j))), "@@@")
            Next j
            eve(i + 1) = "  1"
        Else
            Debug.Print String$(2 * n - 2 * i, " ") & Join(eve, " ")
            odd(1) = "  1"
            ReDim Preserve odd(i + 1)
            For j = 2 To i
                odd(j) = Format(CStr(Val(eve(j - 1)) + Val(eve(j))), "@@@")
            Next j
            odd(i + 1) = "  1"
        End If
    Next i
End Sub
Public Sub main()
    pascal_triangle 13
End Sub




Ada: package Pascal is
   
   type Row is array (Natural range <>) of Natural;
   
   function Length(R: Row) return Positive;
   
   function First_Row(Max_Length: Positive) return Row;
   
   function Next_Row(R: Row) return Row;
   
end Pascal;
Task: Pascal's triangle

C: #include <stdio.h>

void pascaltriangle(unsigned int n)
{
  unsigned int c, i, j, k;

  for(i=0; i < n; i++) {
    c = 1;
    for(j=1; j <= 2*(n-1-i); j++) printf(" ");
    for(k=0; k <= i; k++) {
      printf("%3d ", c);
      c = c * (i-k)/(k+1);
    }
    printf("\n");
  }
}

int main()
{
  pascaltriangle(8);
  return 0;
}
Task: Pascal's triangle



C#: using System;

namespace RosettaCode {

    class PascalsTriangle {

        public static void CreateTriangle(int n) {
            if (n > 0) {
                for (int i = 0; i < n; i++) {
                    int c = 1;
                    Console.Write(" ".PadLeft(2 * (n - 1 - i)));
                    for (int k = 0; k <= i; k++) {
                        Console.Write("{0}", c.ToString().PadLeft(3));
                        c = c * (i - k) / (k + 1);
                    }
                    Console.WriteLine();
                }
            }
        }

        public static void Main() {
            CreateTriangle(8);
        }
    }
}
Task: Pascal's triangle


C++: #include <iostream>
#include <algorithm>
#include<cstdio>
using namespace std;
void Pascal_Triangle(int size) {

	int a[100][100];
	int i, j;

	
	for (i = 1; i <= size; i++) {
		a[i][1] = a[1][i] = 1;
	}
	
	
	for (i = 2; i <= size; i++) {
		for (j = 2; j <= size - i; j++) {
			if (a[i - 1][j] == 0 || a[i][j - 1] == 0) {
				break;
			}
			a[i][j] = a[i - 1][j] + a[i][j - 1];
		}
	}

	
	
	
	
	int row,space;
	for (i = 1; i < size; i++) {
		space=row=i;
		j=1;
		
		while(space<=size+(size-i)+1){
			 cout<<" ";
			 space++;
		 }
		
		while(j<=i){
			if (a[row][j] == 0){
				   break;
			   }
			
			if(j==1){
				printf("%d",a[row--][j++]);
			}
			else
				printf("%6d",a[row--][j++]);
		}
			cout<<"\n\n";
	}
	
}

int main()
{
	
	
	int size;
	cin>>size;
	Pascal_Triangle(size);
}

}
Task: Pascal's triangle





Java: import java.util.ArrayList;
...
public static void genPyrN(int rows){
	if(rows < 0) return;
	
	ArrayList<Integer> last = new ArrayList<Integer>();
	last.add(1);
	System.out.println(last);
	for(int i= 1;i <= rows;++i){
		
		ArrayList<Integer> thisRow= new ArrayList<Integer>();
		thisRow.add(last.get(0)); 
		for(int j= 1;j < i;++j){
			
			thisRow.add(last.get(j - 1) + last.get(j));
		}
		thisRow.add(last.get(0)); 
		last= thisRow;
		System.out.println(thisRow);
	}
}
Task: Pascal's triangle







Python: def pascal(n):
   
   row = [1]
   k = [0]
   for x in range(max(n,0)):
      print row
      row=[l+r for l,r in zip(row+k,k+row)]
   return n>=1
Task: Pascal's triangle








VB: Option Base 1
Private Sub pascal_triangle(n As Integer)
    Dim odd() As String
    Dim eve() As String
    ReDim odd(1)
    ReDim eve(2)
    odd(1) = "  1"
    For i = 1 To n
        If i Mod 2 = 1 Then
            Debug.Print String$(2 * n - 2 * i, " ") & Join(odd, " ")
            eve(1) = "  1"
            ReDim Preserve eve(i + 1)
            For j = 2 To i
                eve(j) = Format(CStr(Val(odd(j - 1)) + Val(odd(j))), "@@@")
            Next j
            eve(i + 1) = "  1"
        Else
            Debug.Print String$(2 * n - 2 * i, " ") & Join(eve, " ")
            odd(1) = "  1"
            ReDim Preserve odd(i + 1)
            For j = 2 To i
                odd(j) = Format(CStr(Val(eve(j - 1)) + Val(eve(j))), "@@@")
            Next j
            odd(i + 1) = "  1"
        End If
    Next i
End Sub
Public Sub main()
    pascal_triangle 13
End Sub