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