-
Notifications
You must be signed in to change notification settings - Fork 0
HomeWork2 #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
HomeWork2 #2
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| // | ||
| // main.swift | ||
| // lesson2 | ||
| // | ||
| // Created by Vladislav Elkin on 15.07.2020. | ||
| // Copyright © 2020 Vladislav Elkin. All rights reserved. | ||
| // | ||
|
|
||
| import Foundation | ||
|
|
||
| //1.Написать функцию, которая определяет, четное число или нет. | ||
|
|
||
| func isEven(_ number: Int) -> Bool { | ||
| return number % 2 == 0 ? true : false; | ||
| } | ||
|
|
||
| print(isEven(10)); | ||
| print(isEven(5)); | ||
| print("\r\n===============================\r\n") | ||
|
|
||
| //2. Написать функцию, которая определяет, делится ли число без остатка на 3. | ||
|
|
||
| func function2(_ number: Int) -> Bool{ | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. давай имена фукнциям более осмысленные |
||
| return number % 3 == 0 ? true : false; | ||
| } | ||
|
|
||
| print(function2(9)); | ||
| print(function2(7)); | ||
| print("\r\n===============================\r\n") | ||
|
|
||
| //3. Создать возрастающий массив из 100 чисел. | ||
|
|
||
| var tArray: [Int] = []; | ||
| for i in 0...100 { | ||
| tArray.append(i) | ||
| } | ||
|
|
||
| print(tArray) | ||
| print("\r\n===============================\r\n") | ||
|
|
||
| //4. Удалить из этого массива все четные числа и все числа, которые не делятся на 3. | ||
|
|
||
| for(index, value) in tArray.enumerated(){ | ||
| if isEven(value) || !function2(value) { | ||
| tArray.remove(at: tArray.firstIndex(of: value)!); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. tArray.firstIndex(of: value)? Ты ведь уже index получаешь |
||
| } | ||
| } | ||
|
|
||
| print(tArray) | ||
|
|
||
| print("\r\n===============================\r\n") | ||
|
|
||
| //5. * Написать функцию, которая добавляет в массив новое число Фибоначчи, и добавить при помощи нее 100 элементов. | ||
|
|
||
| func fibonacci(_ n: Int64) -> Int64{ | ||
| if n == 1 || n == 2 { | ||
| return 1; | ||
| } | ||
|
|
||
| return fibonacci(n - 1) + fibonacci(n - 2); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. рекурсивная функция считается не очень оптимальной. Посмотри есть материалы, что при достаточно большом числе все пойдет по....ну ты знаешь:) |
||
| } | ||
|
|
||
| var fibonacciArray: [Int64] = []; | ||
|
|
||
| for i in 1...101{ | ||
| fibonacciArray.append(fibonacci(Int64(i))); | ||
| } | ||
|
|
||
| print(fibonacciArray); | ||
|
|
||
| print("\r\n===============================\r\n") | ||
|
|
||
| /* | ||
| 6. * Заполнить массив из 100 элементов различными простыми числами. Натуральное число, большее единицы, называется простым, если оно делится только на себя и на единицу. Для нахождения всех простых чисел не больше заданного числа n, следуя методу Эратосфена, нужно выполнить следующие шаги: | ||
| */ | ||
|
|
||
| func isPrime (_ number: Int64) -> Bool { | ||
| if number < 2 { | ||
| return false; | ||
| } | ||
| for i in 2..<number { | ||
| if number % i == 0 { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| return true | ||
| } | ||
| func getPrimeArray (_ count: Int64) -> [Int64] { | ||
| var resultArray: [Int64] = []; | ||
| var i = 2 | ||
| while resultArray.count < count { | ||
| if isPrime(Int64(i)) { | ||
| resultArray.append(Int64(i)); | ||
| } | ||
| i += 1; | ||
| } | ||
|
|
||
| return resultArray; | ||
| } | ||
| print (getPrimeArray(100)); | ||
|
|
||
| print("\r\n===============================\r\n") | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| // | ||
| // main.swift | ||
| // test1 | ||
| // | ||
| // Created by Vladislav Elkin on 13.07.2020. | ||
| // Copyright © 2020 Vladislav Elkin. All rights reserved. | ||
| // | ||
|
|
||
| import Foundation | ||
|
|
||
| //1. Решить квадратное уравнение. | ||
|
|
||
| let a:Double = 1; | ||
| let b:Double = -8; | ||
| let c:Double = 15; | ||
| let discriminant = (b * b) - (4 * a * c); | ||
|
|
||
| if discriminant < 0 { | ||
| print("Корней нет!"); | ||
| }else if (discriminant == 0){ | ||
| let x = (-b + sqrt(discriminant)) / (2 * a); | ||
| print("x = \(x)"); | ||
| }else{ | ||
| let x1 = (-b + sqrt(discriminant)) / (2 * a); | ||
| let x2 = (-b - sqrt(discriminant)) / (2 * a); | ||
|
|
||
| print("x1 = \(x1), x2 = \(x2)"); | ||
| } | ||
|
|
||
|
|
||
| //2. Даны катеты прямоугольного треугольника. Найти площадь, периметр и гипотенузу треугольника. | ||
|
|
||
| let catA: Double = 5; | ||
| let catB: Double = 6; | ||
|
|
||
| let gipC = sqrt((catA * catA) + (catB * catB)); | ||
| let S = 0.5 * catB * catA; | ||
| let P = catB + catA + gipC; | ||
|
|
||
| print("P = \(P), S = \(S), C = \(gipC)") | ||
|
|
||
| //3. *Пользователь вводит сумму вклада в банк и годовой процент. Найти сумму вклада через 5 лет. | ||
|
|
||
| var deposit: Double = 1000; | ||
| var years: Double = 5; | ||
| let procent: Double = 7; | ||
|
|
||
| while years > 0 { | ||
| deposit += deposit * procent / 100; | ||
| years-=1; | ||
| } | ||
|
|
||
| print(deposit); | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
можно скоратить до
return number % 2 == 0