Делаем буквально то, что написано в условии: получаем список простых делителей для каждого числа, если их оказалось три и все они различные, то проверяем, не оканчиваются ли все они на одну цифру. Если и это совпало, добавляем это число в список. В конце выводим ответ: количество чисел в списке и разность между максимальным и минимальным значением.
Производительность тут не так важна, так что можно ничего не оптимизировать, и так ответ получается за разумное время.
Код (python 3):
answer = []
for number in range(416782, 498324 + 1):
prime_divisors, possible_divisor, current = [], 2, number
while possible_divisor ** 2 <= current:
if current % possible_divisor == 0:
prime_divisors.append(possible_divisor)
current //= possible_divisor
else:
possible_divisor += 1
if current != 1:
prime_divisors.append(current)
if len(prime_divisors) == 3 and len(set(prime_divisors)) == 3:
last_digit = prime_divisors[0] % 10
if all(p % 10 == last_digit for p in prime_divisors):
answer.append(number)
print(len(answer))
print(max(answer) - min(answer))
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
int a= Int32.Parse(Console.ReadLine());
int b = Int32.Parse(Console.ReadLine());
int c = Int32.Parse(Console.ReadLine());
int d = Int32.Parse(Console.ReadLine());
int e = Int32.Parse(Console.ReadLine());
int f = Int32.Parse(Console.ReadLine());
int g = Int32.Parse(Console.ReadLine());
int h = Int32.Parse(Console.ReadLine());
b = Math.Max(a, b);
c = Math.Max(b, c);
d = Math.Max(c, d);
e = Math.Max(d, e);
f = Math.Max(e, f);
g = Math.Max(f, g);
h = Math.Max(g, h);
Console.WriteLine(h);
}
}
}