// C# implementation to find the row
// whose product has maximum number
// of prime factors
using System;
using System.Collections.Generic;
class GFG{
static readonly int N = 3;
static readonly int M = 5;
static int Large = (int) 1e6;
static List<int> prime = new List<int>();
// function for SieveOfEratosthenes
static void SieveOfEratosthenes()
{
// Create a bool array "isPrime[0..N]"
// and initialize all entries it as true.
// A value in isPrime[i] will finally be
// false if i is not a prime, else true.
bool []isPrime = new bool[Large + 1];
for (int p = 0; p <= Large; p++)
isPrime[p] = true;
for (int p = 2; p * p <= Large; p++)
{
// check if isPrime[p] is not changed
if (isPrime[p] == true)
{
// Update all multiples of p
for (int i = p * 2; i <= Large; i += p)
isPrime[i] = false;
}
}
// Print all isPrime numbers
for (int p = 2; p <= Large; p++)
if (isPrime[p])
prime.Add(p);
}
// function to display the answer
static void Display(int [, ]arr, int row)
{
for (int i = 0; i < M; i++)
Console.Write(arr[row, i] + " ");
}
// function to Count the row number of
// divisors in particular row multiplication
static void countDivisorsMult(int [, ]arr)
{
// Find count of occurrences
// of each prime factor
Dictionary<int,
int> mp = new Dictionary<int,
int>();
int row_no = 0;
long max_factor = 0;
for (int i = 0; i < N; i++)
{
for (int j = 0; j < M; j++)
{
int no = arr[i,j];
for (int k = 0; k < prime.Count; k++)
{
while (no > 1 && no %
prime[k] == 0)
{
no /= prime[k];
if(mp.ContainsKey(prime[k]))
mp[prime[k]] = prime[k] + 1;
else
mp.Add(prime[k], 1);
}
if (no == 1)
break;
}
}
// Compute count of all divisors
int res = 1;
foreach (KeyValuePair<int,int> it in mp)
{
res *= (it.Value + 1);
}
// Update row number if
// factors of this row is max
if (max_factor < res)
{
row_no = i;
max_factor = res;
}
// Clearing map to store
// prime factors for next row
mp.Clear();
}
Display(arr, row_no);
}
// Driver code
public static void Main(String[] args)
{
int [, ]arr = {{1, 2, 3, 10, 23},
{4, 5, 6, 7, 8},
{7, 8, 9, 15, 45}};
SieveOfEratosthenes();
countDivisorsMult(arr);
}
}
// This code is contributed by Rajput-Ji