VisualStudio 2019 의 메뉴에서 "도구(T)" -> "명령줄(L)" -> "개발자 PowerShell(P)" 을 선택하거나

윈도우 10에서 '윈도우 키' + 'R' 을 누른 다음 명령어 입력 난에 powershell 을 입력하여

나타난 PowerShell 창에서 다음을 입력한다.

 

function multarrays($a, $b) {
    $n,$m,$p = ($a.Count - 1), ($b.Count - 1), ($b[0].Count - 1)
    if ($a[0].Count -ne $b.Count) {throw "Multiplication impossible"}
    $c = @(0)*($a[0].Count)
    foreach ($i in 0..$n) {    
        $c[$i] = foreach ($j in 0..$p) { 
            $sum = 0
            foreach ($k in 0..$m){$sum += $a[$i][$k]*$b[$k][$j]}
            $sum
        }
    }
    $c
 }
 
function show($a) { $a | foreach{"$_"}}

 

계속해서 다음을 입력한다.

$a = @(@(1,2),@(3,4))
$b = @(@(5,6),@(7,8))
$c = @(5,6)

 

이제 다음을 한 줄씩 입력하면 각각의 곱셈 결과가 표시된다.

"`$a ="; show $a
""
"`$b ="; show $b
""
"`$c ="; $c
""
"`$a * `$b ="; show (multarrays $a $b)
""
"`$a * `$c ="; show (multarrays $a $c)

 

 

[참고 자료] Rosetta Code - Matrix Mulriplication

'프로그래밍 > PowerShell' 카테고리의 다른 글

0.3 - 0.2 - 0.1 != 0.0  (0) 2022.02.09
Posted by Scripter
,

 

C 언어나 C++ 언어에는 세제곱근을 구하는 함수 cbrt() 가 기본적으로 제공되어 있다.

소스에서 단지 #include <math.h> 또는 #include <cmath> 를 추가하기만 된다.

그러나 C# 언어에는 이런 함수가 기본적으로 제공되지 있지 않다. (Framework 의 경우)

그런데 Core 3.0 이상의 경우에는 C# 언어에서도 제곱근 함수 Sqrt() 와 세제곱근 함수 Cbrt() 를

기본적으로 사용할 수 있다. 

using System.Math;  구문이 없더라도 쓸 수 있다.

 

Visual Studion 2019에서 "새 프로젝트 만들기" -> "콘솔 앱(.NET Core)" 하고

프로젝트 이름을 적당히 써 주고 C# 소스 Progam.cs 를 편집하는 창에서

Main() 함수 부분을 다음과 같이 수정하고 빌드하여 실행한다.

 

static void Main(string[] args)
{
    // Calculate the cubic root of a single preceson type.
    float x = 27f;
    float y = MathF.Cbrt(x);
    Console.WriteLine("Cube root of {0} is {1}", x, y);
    
    // Calculate the cubic root of a double preceson type.
    double x2 = 0.000000000000125;
    double y2 = Math.Cbrt(x2);
    Console.WriteLine("Cube root of {0:F15} is {1:F5}", x2, y2);
    
    Console.Write("Press any key... ");
    Console.ReadLine();
}

 

그러면 컨솔 창에 실행 결과가 다음 처럼 출력된다.

Cube root of 27 is 3
Cube root of 0.000000000000125 is 0.00005
Press any key...

 

 

 

Posted by Scripter
,

참고: Special Pythagorean triplet Problem 9

 

아래는 문제 해결에 도움되는 유효한 (정수론의 어떠헌) 정리도 사용하지 않고,

단순히 중첩된 for 반복문을 사용한 (이해하기 쉬운) C# 소스와 그 실행 결과이다.

 

C# 소스:

// Filename: PainSolutionEulerProjectNum9.cs
//
// Compile: csc PainSolutionEulerProjectNum9.cs
//
// Execute: PainSolutionEulerProjectNum9
//
// See: https://projecteuler.net/problem=9

using System;

namespace GeneralCommandLineApp
{
   class Program
   {
        static void Main(string[] args)
        {
        	int c;
        	for (int a = 1; a <=1000/3; a++)
        	{
        	    for (int b = a;  a + b < 1000; b++)
        	    {
        		  c = 1000 - a - b;
        		  if (a*a + b*b == c*c && a + b > c) {
        		  	  Console.WriteLine("a = {0}, b = {1}, c = {2}", a, b, c);
        		  	  Console.WriteLine("And so a + b + c = {0} + {1} + {2} = {3}", a, b, c, a + b + c);
        		  	  Console.WriteLine("So their product is a*b*c = {0}*{1}*{2} = {3}", a, b, c, a*b*c);
        		  	  Console.WriteLine("Primitive Pythagorean triplet:");
        		  	  Console.WriteLine();

        		  	  int a2, b2, c2;
        		  	  a2 = a/25;
        		  	  b2 = b/25;
        		  	  c2 = c/25;
        		  	  int g = (int) gcd(a, b, c);
        		  	  Console.WriteLine("    g = gcd({0}, {1}, {2}) = {3}", a, b, c, g);
        		  	  Console.WriteLine("    a/g = {0}, b/g = {1}, c/g= {2}", a/g, b/g, c/g);
        		  	  Console.WriteLine("    {0}, {1},  {2}", a2, b2, c2);
        		  	  Console.WriteLine("    {0}^2 + {1}^2 = {3} + {4} = {5} = {2}^2", a2, b2, c2, a2*a2, b2*b2, c2*c2);
        		  }
                 }
            }
        }
            
        static Int64 gcd(Int64 x, Int64 y, Int64 z)
        {
        	Int64 w = EuclidGCD(x, y);
        	Int64 v = EuclidGCD(w, z);
        	return v;
        }
            
        static Int64 EuclidGCD(Int64 x, Int64 y)
        {
        	Int64 a, b, r;

        	a = x < 0 ? -x : x;
        	b = y < 0 ? -y : y;
        	
        	r = b;
             while (r > 0) {
                 b = a % r;
                 a = r;
                 r = b;
             }
             return a;
        }
    }
}

 

실행 결과:

a = 200, b = 375, c = 425
And so a + b + c = 200 + 375 + 425 = 1000
So their product is a*b*c = 200*375*425 = 31875000
Primitive Pythagorean triplet:

    g = gcd(200, 375, 425) = 25
    a/g = 8, b/g = 15, c/g= 17
    8, 15,  17
    8^2 + 15^2 = 64 + 225 = 289 = 17^2

 

 

Posted by Scripter
,

 

 

원시 피타고라스 삼조(primitive pythagorea triplet)를 생성하는

명령줄 어플(Command Line Application) C# 소스

 

C# 소스:

// Filename: GeneratePrimitivePythagoreanTriplets.cs
//
// Compile: csc GeneratePrimitivePythagoreanTriplets.cs
//
// Execute: GeneratePrimitivePythagoreanTriplets 7

using System;
using System.Collections.Generic;

namespace GeneralCommandLineApp
{
   class Program
    {
    	   public static Int64 GetGCD(Int64 xa, Int64 xb)
    	  {
    	  	  Int64 a = Math.Abs(xa);
    	  	  Int64 b = Math.Abs(xb);
    	  	  Int64 c;
    	  	  if (b > a) {
    	  	  	  c = b;
    	  	  	  b = a;
    	  	  	  a = c;
    	  	  }
    	  	  Int64 r;
    	  	  r = b;
    	  	  while (r > 0)
    	  	  {
    	  	  	  r = a % b;
    	  	  	  a = b;
    	  	  	  b = r;
    	  	  }
    	  	  return a;
    	  }

    	  public static void GeneratePrimitivePythagoreanTriplets(List<PythagoreanTriplet> data, Int64 k)
    	  {
    	  	  for (Int64 m = 2; m <= k; m++)
    	  	  {
    	  	      for (Int64 n = 1; n < m; n++)
    	  	      {
    	  	      	     if (GetGCD(m, n) == 1 && ((m % 2 == 0 && n % 2 != 0) || (m % 2 != 0 && n % 2 == 0))) {
    	  	  	         data.Add(new PythagoreanTriplet(m, n));
    	  	  	     }
    	  	      }
    	  	  }
        }

    	  public static void PrintTripletsAsTableForm(List<PythagoreanTriplet> data)
    	  {
    	  	  Int64 k = data.Count;
    	  	  Int64 a, b, c, m, n;
    	  	  Console.WriteLine("Print out some primitive Pythagorean triplets (a, b, c)\neach of which satisfies a^2 + b^2 = c^2.");
    	  	  Console.WriteLine();
    	  	  Console.WriteLine("      +--------+--------+-----------+-----------+-----------+");
    	  	  Console.WriteLine("      |        |        | m^2 - n^2 |     2mn   | m^2 + n^2 |");
    	  	  Console.WriteLine("      |     m  |     n  |      a    |      b    |      c    |");
    	  	  Console.WriteLine("      +--------+--------+-----------+-----------+-----------+");
    	  	  for (int i = 0; i < k; i++) {
    	  	      a = data[i].GetA();
    	  	  b = data[i].GetB();
    	  	  c = data[i].GetC();
    	  	  m = (Int64) Math.Sqrt((a + c)/2);
    	  	  n = b/(2*m);
    	  	  Console.WriteLine("      | {0, 6} | {1, 6} | {2, 9} | {3, 9} | {4, 9} |", m, n, a, b, c);
               }
               Console.WriteLine("      +--------+--------+-----------+-----------+-----------+");
        }

        static void Main(string[] args)
        {
        	Int64 c;
        	if (args.Length == 1) {
                c = Convert.ToInt64(args[0]);
                if (c < 1) {
                     Console.WriteLine("Sorry, you should a positive integer as an upper boud.");
                     return;
                }
                else {
                    List<PythagoreanTriplet> data = new List<PythagoreanTriplet>();
                    GeneratePrimitivePythagoreanTriplets(data, c);
                    PrintTripletsAsTableForm(data);
                    return;
                }
            }
            else
                Console.WriteLine("Usage:  GeneratePrimitivePythagoreanTriplets [UpperBound]");
        }
    }


     class PythagoreanTriplet {
        Int64 a, b, c;
        public PythagoreanTriplet(Int64 m, Int64 n) {
           	this.a = m*m - n*n;
            	this.b = 2*m*n;;
           	this.c = m*m + n*n;
       }
        public Int64 GetA() {
        	return this.a;
        }
        public Int64 GetB() {
        	return this.b;
        }
        public Int64 GetC() {
        	return this.c;
        }

        public override string ToString() {
        	return "(" + this.a + ", " + this.b + ", " + this.c + ")";
        }

        public bool IsPrimitive(Int64 xa, Int64 xb, Int64 xc)
        {
            return GCD(xa, xb, xc) == 1;
        

        public Int64 GCD(Int64 xa, Int64 xb)
        {
            Int64 a = Math.Abs(xa);
            Int64 b = Math.Abs(xb);
            Int64 c;
            if (b > a) {
                c = b;
                b = a;
                a = c;
            }
            Int64 r;
            r = b;
            while (r > 0)
            {
                r = a % b;
                 a = b;
                b = r;
            }
            return a;
        }

        public Int64 GCD(Int64 xa, Int64 xb, Int64 xc)
        {
            Int64 t = GCD(xa, xb);
            Int64 y = GCD(t, xc);
            return y;
        }
    }
}

 

명령 GeneratePrimitivePythagoreanTriplets 7 에 의한 실행 결과:

Print out some primitive Pythagorean triplets (a, b, c)
each of which satisfies a^2 + b^2 = c^2.

      +--------+--------+-----------+-----------+-----------+
      |        |        | m^2 - n^2 |     2mn   | m^2 + n^2 |
      |     m  |     n  |      a    |      b    |      c    |
      +--------+--------+-----------+-----------+-----------+
      |      2 |      1 |         3 |         4 |         5 |
      |      3 |      2 |         5 |        12 |        13 |
      |      4 |      1 |        15 |         8 |        17 |
      |      4 |      3 |         7 |        24 |        25 |
      |      5 |      2 |        21 |        20 |        29 |
      |      5 |      4 |         9 |        40 |        41 |
      |      6 |      1 |        35 |        12 |        37 |
      |      6 |      5 |        11 |        60 |        61 |
      |      7 |      2 |        45 |        28 |        53 |
      |      7 |      4 |        33 |        56 |        65 |
      |      7 |      6 |        13 |        84 |        85 |
      +--------+--------+-----------+-----------+-----------+

 

Posted by Scripter
,