From befd6ee726b746b316bd44309bfab728f14dcf5e Mon Sep 17 00:00:00 2001 From: Artem-Darius Weber Date: Thu, 17 Apr 2025 17:43:58 +0300 Subject: [PATCH] =?UTF-8?q?(lab=205)=20feat:=20task-16-method-1:=20=D0=94?= =?UTF-8?q?=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD=20=D0=BC=D0=B5=D1=82?= =?UTF-8?q?=D0=BE=D0=B4=20=D0=BF=D0=BE=D0=B4=D1=81=D1=87=D0=B5=D1=82=D0=B0?= =?UTF-8?q?=20=D0=B4=D0=B5=D0=BB=D0=B8=D1=82=D0=B5=D0=BB=D0=B5=D0=B9=20?= =?UTF-8?q?=D1=87=D0=B8=D1=81=D0=BB=D0=B0,=20=D0=BD=D0=B5=20=D0=B4=D0=B5?= =?UTF-8?q?=D0=BB=D1=8F=D1=89=D0=B8=D1=85=D1=81=D1=8F=20=D0=BD=D0=B0=203?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- compose.yaml | 9 +++ lab 5/Makefile | 14 +++- .../NumberOperationsAdvanced.fsproj | 13 ++++ lab 5/NumberOperationsAdvanced/Program.fs | 70 +++++++++++++++++++ 4 files changed, 104 insertions(+), 2 deletions(-) create mode 100644 lab 5/NumberOperationsAdvanced/NumberOperationsAdvanced.fsproj create mode 100644 lab 5/NumberOperationsAdvanced/Program.fs diff --git a/compose.yaml b/compose.yaml index 07b0eeb..0648540 100644 --- a/compose.yaml +++ b/compose.yaml @@ -143,4 +143,13 @@ services: volumes: - ./lab 5/ConditionalCoprimeTraversal:/app stdin_open: true + tty: true + + lab5-number-operations-advanced: + build: + context: ./lab 5/NumberOperationsAdvanced + dockerfile: ../../Individual Task 1/Dockerfile + volumes: + - ./lab 5/NumberOperationsAdvanced:/app + stdin_open: true tty: true \ No newline at end of file diff --git a/lab 5/Makefile b/lab 5/Makefile index 9792fd1..5c87231 100755 --- a/lab 5/Makefile +++ b/lab 5/Makefile @@ -1,4 +1,4 @@ -.PHONY: run build clean quadratic circle numbers recursion factory traverse lambda conditional examples favoritelang favoritelangfp mutuallyprimetraversal eulerfunction conditionalcoprimetraversal +.PHONY: run build clean quadratic circle numbers recursion factory traverse lambda conditional examples favoritelang favoritelangfp mutuallyprimetraversal eulerfunction conditionalcoprimetraversal numberoperationsadvanced # HelloWorld run: @@ -150,4 +150,14 @@ build-conditionalcoprimetraversal: cd ConditionalCoprimeTraversal && dotnet build clean-conditionalcoprimetraversal: - cd ConditionalCoprimeTraversal && dotnet clean \ No newline at end of file + cd ConditionalCoprimeTraversal && dotnet clean + +# NumberOperationsAdvanced +numberoperationsadvanced: + cd NumberOperationsAdvanced && dotnet run --project NumberOperationsAdvanced.fsproj + +build-numberoperationsadvanced: + cd NumberOperationsAdvanced && dotnet build + +clean-numberoperationsadvanced: + cd NumberOperationsAdvanced && dotnet clean \ No newline at end of file diff --git a/lab 5/NumberOperationsAdvanced/NumberOperationsAdvanced.fsproj b/lab 5/NumberOperationsAdvanced/NumberOperationsAdvanced.fsproj new file mode 100644 index 0000000..c5f282e --- /dev/null +++ b/lab 5/NumberOperationsAdvanced/NumberOperationsAdvanced.fsproj @@ -0,0 +1,13 @@ + + + + Exe + net7.0 + NumberOperationsAdvanced + + + + + + + \ No newline at end of file diff --git a/lab 5/NumberOperationsAdvanced/Program.fs b/lab 5/NumberOperationsAdvanced/Program.fs new file mode 100644 index 0000000..9729e4e --- /dev/null +++ b/lab 5/NumberOperationsAdvanced/Program.fs @@ -0,0 +1,70 @@ +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 "" + +[] +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 \ No newline at end of file