yanchevilian

09. List of Predicates / Functional Programming

Sep 7th, 2021
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.08 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Channels;
  5.  
  6. namespace _9._List_of_Predicates
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             int endRange = int.Parse(Console.ReadLine());
  13.             int[] dividers = Console.ReadLine().Split().Select(int.Parse).ToArray();
  14.             List<int> dividedNumbersInRange = new List<int>();
  15.  
  16.             Func<int[], int, bool> filter = (allDividers, number) =>
  17.             {
  18.                 bool isDivisible = true;
  19.  
  20.                 for (int i = 0; i < allDividers.Length; i++)
  21.                 {
  22.                     if (number % allDividers[i] != 0)
  23.                     {
  24.                         isDivisible = false;
  25.                         break;
  26.                     }
  27.                 }
  28.  
  29.                 return isDivisible;
  30.             };
  31.             int[] divisibleNumbers = Enumerable.Range(1, endRange).Where(number => filter(dividers, number)).ToArray();
  32.  
  33.             Console.WriteLine(string.Join(" ", divisibleNumbers));
  34.         }
  35.     }
  36. }
  37.  
Advertisement
Add Comment
Please, Sign In to add comment