-
Notifications
You must be signed in to change notification settings - Fork 7
/
Program.cs
53 lines (50 loc) · 1.26 KB
/
Program.cs
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
using System;
using System.Text;
namespace SuperPalindromes
{
class Program
{
static void Main(string[] args)
{
string left = "1000000000000000000";
Console.WriteLine(long.Parse(left));
}
public int SuperpalindromesInRange(string left, string right)
{
long limit = 1_00_000;
int res = 0;
for (long i = 1; i < limit; i++)
{
string frontHalf = i.ToString();
var str1 = new StringBuilder(frontHalf);
var str2 = new StringBuilder(frontHalf);
for (int j = frontHalf.Length - 2; j >= 0; j--)
str1.Append(frontHalf[j]);
var num = long.Parse(str1.ToString());
for (int j = frontHalf.Length - 1; j >= 0; j--)
str2.Append(frontHalf[j]);
if ((num * num).ToString().Length > right.Length) break;
if (IsValid(str1, left, right))
res++;
if (IsValid(str2, left, right))
res++;
}
return res;
}
public bool IsValid(StringBuilder str, string left, string right)
{
long num = long.Parse(str.ToString());
return num * num >= long.Parse(left) && num * num <= long.Parse(right) && IsPalindrome((num * num).ToString());
}
public bool IsPalindrome(string str)
{
int li = 0, hi = str.Length - 1;
while (li < hi)
{
if (str[li++] != str[hi--])
return false;
}
return true;
}
}
}