Простое решение является парой взаимно рекурсивный функции.
первая функция генерирует все простые числа:
вторая функция возвращает простые множители данного номера n
в увеличивающемся порядке.
n
. самый большой простой множитель n
является последним числом, данным второй функцией.
Этот алгоритм требует ленивый список или язык (или структура данных) с вызов по необходимости семантика.
Для разъяснения, вот одна (неэффективная) реализация вышеупомянутого в Haskell:
import Control.Monad
-- All the primes
primes = 2 : filter (ap (<=) (head . primeFactors)) [3,5..]
-- Gives the prime factors of its argument
primeFactors = factor primes
where factor [] n = []
factor xs@(p:ps) n =
if p*p > n then [n]
else let (d,r) = divMod n p in
if r == 0 then p : factor xs d
else factor ps n
-- Gives the largest prime factor of its argument
largestFactor = last . primeFactors
Создание это быстрее - просто вопрос того, чтобы быть более умным об обнаружении, какие числа являются главными и/или факторы n
, но алгоритм остается таким же.
Depending on what the 3rd party process is doing exactly you could try polling its threads' states:
foreach(ProcessThread thread in process.Threads)
if (thread.ThreadState == ThreadState.Wait
&& thread.WaitReason == ThreadWaitReason.UserRequest)
process.Kill();
Failing that... you can try to
process.StandardInput.Close();
after calling Start(), I conjecture that an exception will be raised in the child process if it's trying to read from standard input.
If the console application has some sort of prompt waiting for input, you could periodically parse out the console output text using the Process.StandardOutput property of the process and wait for said prompt. Once the proper string is detected, you know that it's waiting for input. See http://msdn.microsoft.com/en-us/library/system.diagnostics.process.standardoutput.aspx.