-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBestDivisor.cs
More file actions
58 lines (55 loc) · 1.48 KB
/
BestDivisor.cs
File metadata and controls
58 lines (55 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HackerrankSolution
{
public class BestDivisor
{
private static int GetBestDivisor(int n)
{
int result = 0;
int sumOfDigit = 0;
for(int i=1; i<=n; i++)
{
if (n % i == 0)
{
int currentSumOfDigit = GetSumOfDigit(i);
if (sumOfDigit == currentSumOfDigit)
{
if (i > result)
{
result = i;
sumOfDigit = currentSumOfDigit;
}
}
else
{
if (currentSumOfDigit > sumOfDigit)
{
result = i;
sumOfDigit = currentSumOfDigit;
}
}
}
}
return result;
}
private static int GetSumOfDigit(int n)
{
int sum = 0;
while (n > 0)
{
sum += n % 10;
n = n / 10;
}
return sum;
}
public static void Execute()
{
int n = Convert.ToInt32(Console.ReadLine());
Console.WriteLine(GetBestDivisor(n));
}
}
}