marekov

PrimeTriangle

May 21st, 2020
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.22 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace PrimeTriangle
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             int n = int.Parse(Console.ReadLine());
  11.             List<int> primes = new List<int>();
  12.             int count = 1;
  13.  
  14.             for (int i = 2; i <= n; i++)
  15.             {
  16.                 count = 0;
  17.                 for (int j = 1; j <= i; j++)
  18.                 {
  19.                     if (i % j == 0)
  20.                     {
  21.                         count++;
  22.                     }
  23.                 }
  24.                 if (count == 2)
  25.                 {
  26.                     primes.Add(i);
  27.                 }
  28.             }
  29.             primes.Insert(0, 1);
  30.             for (int rows = 1; rows <= primes.Count; rows++)
  31.             {
  32.                 for (int cols = 1; cols <= primes[rows - 1]; cols++)
  33.                 {
  34.                     if (primes.Contains(cols))
  35.                     {
  36.                         Console.Write("1");
  37.                     }
  38.                     else
  39.                     {
  40.                         Console.Write("0");
  41.                     }
  42.                 }
  43.                 Console.WriteLine();
  44.             }
  45.         }
  46.     }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment