You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

70 lines
3.1 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

open System
// method 1: Найти количество делителей числа, не делящихся на 3
let countDivisorsNotDivisibleByThree (n: int) =
let rec countDivisors current count =
if current > n then
count
else
// является ли текущее число делителем n и не делится ли оно на 3
let newCount =
if n % current = 0 && current % 3 <> 0 then
count + 1
else
count
countDivisors (current + 1) newCount
if n <= 0 then 0 else countDivisors 1 0
// method test
let testCountDivisorsNotDivisibleByThree () =
let testCases = [1; 3; 6; 9; 10; 12; 15; 20; 30]
printfn "Тестирование метода 1: Количество делителей числа, не делящихся на 3"
for n in testCases do
let divisors = [1..n] |> List.filter (fun i -> n % i = 0)
let divisorsNotDivByThree = divisors |> List.filter (fun i -> i % 3 <> 0)
let expected = divisorsNotDivByThree.Length
let result = countDivisorsNotDivisibleByThree n
printfn "n = %d:" n
printfn " Все делители: %A" divisors
printfn " Делители, не делящиеся на 3: %A" divisorsNotDivByThree
printfn " Результат: %d (ожидаемый: %d) - %s" result expected (if result = expected then "OK" else "ОШИБКА")
printfn ""
[<EntryPoint>]
let main argv =
Console.OutputEncoding <- Text.Encoding.UTF8
printfn "Задание 16. Вариант 5. Метод 1: Найти количество делителей числа, не делящихся на 3.\n"
testCountDivisorsNotDivisibleByThree()
printfn "Интерактивный режим:"
let rec processUserInput () =
printf "Введите число для вычисления количества делителей, не делящихся на 3 (или 'q' для выхода): "
let input = Console.ReadLine()
match input.ToLower() with
| "q" | "quit" | "exit" -> ()
| _ ->
match Int32.TryParse(input) with
| true, n when n > 0 ->
let result = countDivisorsNotDivisibleByThree n
let divisors = [1..n] |> List.filter (fun i -> n % i = 0)
let divisorsNotDivByThree = divisors |> List.filter (fun i -> i % 3 <> 0)
printfn "Число: %d" n
printfn "Все делители: %A" divisors
printfn "Делители, не делящиеся на 3: %A" divisorsNotDivByThree
printfn "Количество делителей, не делящихся на 3: %d" result
printfn ""
processUserInput ()
| _ ->
printfn "Некорректный ввод. Пожалуйста, введите положительное целое число.\n"
processUserInput ()
processUserInput ()
0