-
Notifications
You must be signed in to change notification settings - Fork 102
Lesson 1-8 #98
Lesson 1-8 #98
Changes from 1 commit
44f9bc5
4aa2806
cbe1e7a
3eb5fc8
7f470e5
35779dc
3384df0
c8fcb64
796fb0e
457f2b5
0319e2a
e95dcc4
08b0657
8927e6c
38e230e
abe1b5b
b059ac6
de78efc
7000f0d
89ac9eb
0232594
cab3135
e4d6f83
6802b1c
734e55b
bd1b46f
31ffe5c
9ec8e6a
8adc830
b6ad932
01bcdca
84e1d67
0edaab8
3bcfd51
7c8d720
c3bf33c
908d662
e214e03
83e69d5
5957093
46278cc
54f3f4a
588d2d0
606111e
c188e61
e53dbb6
4a6feae
823b394
23a300c
7e85fb0
aa35eed
8baa5e1
bcf2115
c3097c2
9784803
481ed4c
a257dcd
d126204
02ca56b
58ff99c
d2c51b4
dd34a0c
03c9241
4f76c74
c61039f
fed7262
06a2864
abcfbe4
f3a6ad6
d76b01e
6160f5b
4e31add
e22b67e
e8684d5
c296b9a
f47c249
25e59d3
7b99edf
c52c2f3
550d58f
ff6202b
031dde7
cd63897
da0925d
58690c7
4308a60
9b65e1f
43c8cc3
bf680dc
22e1aa6
3aab577
45bafd3
709ea9e
26b278e
2d9d1c2
c597566
2bdf250
c5df9ab
caa541b
3d6a9e5
177e402
146b3e3
a1c004b
a335c90
09cba88
0977dab
87a4210
b927f71
3dac39f
6d8727c
1414cd0
6b6db97
adce763
2f08c76
ec92b7d
7d2c778
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -77,8 +77,19 @@ fun digitNumber(n: Int): Int { | |
* Найти число Фибоначчи из ряда 1, 1, 2, 3, 5, 8, 13, 21, ... с номером n. | ||
* Ряд Фибоначчи определён следующим образом: fib(1) = 1, fib(2) = 1, fib(n+2) = fib(n) + fib(n+1) | ||
*/ | ||
fun fib(n: Int): Int = if (n > 2) (fib(n - 1) + fib(n - 2)) else 1 | ||
|
||
fun fib(n: Int): Int { | ||
var help1 = 1 | ||
var help2 = 1 | ||
var res = 0 | ||
for (i in 1..n) { | ||
if (i > 2) { | ||
res = help1 + help2 | ||
help1 = help2 | ||
help2 = res | ||
} else res = 1 | ||
} | ||
return res | ||
} | ||
|
||
/** | ||
* Простая | ||
|
@@ -97,9 +108,8 @@ fun qcd(n: Int, m: Int): Int { | |
return min | ||
} | ||
|
||
fun lcm(m: Int, n: Int): Int { | ||
return m * n / qcd(n, m) | ||
} | ||
fun lcm(m: Int, n: Int): Int = m * n / qcd(n, m) | ||
|
||
|
||
|
||
/** | ||
* Простая | ||
|
@@ -141,7 +151,7 @@ fun maxDivisor(n: Int): Int { | |
* Например, 25 и 49 взаимно простые, а 6 и 8 -- нет. | ||
*/ | ||
|
||
fun isCoPrime(m: Int, n: Int): Boolean = lcm(m, n) == m * n | ||
fun isCoPrime(m: Int, n: Int): Boolean = qcd(m, n) == 1 | ||
|
||
/** | ||
* Простая | ||
|
@@ -151,9 +161,9 @@ fun isCoPrime(m: Int, n: Int): Boolean = lcm(m, n) == m * n | |
* Например, для интервала 21..28 21 <= 5*5 <= 28, а для интервала 51..61 квадрата не существует. | ||
*/ | ||
fun squareBetweenExists(m: Int, n: Int): Boolean { | ||
val mSqrt = Math.floor(Math.sqrt(m.toDouble())) | ||
val nSqrt = Math.ceil(Math.sqrt(n.toDouble())) | ||
if (nSqrt - mSqrt > 1 || m == 0 || n == 0 || m == 1 || n == 1) return true | ||
val mSqrt = Math.ceil(Math.sqrt(m.toDouble())) | ||
val nSqrt = Math.floor(Math.sqrt(n.toDouble())) | ||
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. Браво! 👍 |
||
if (nSqrt - mSqrt >= 0) return true | ||
|
||
else return false | ||
} | ||
|
||
|
@@ -247,15 +257,8 @@ fun squareSequenceDigit(n: Int): Int { | |
fun fibSequenceDigit(n: Int): Int { | ||
var k = "" | ||
var count = 0 | ||
var help1 = 1 | ||
var help2 = 1 | ||
|
||
for (i in 1..n) { | ||
if (i > 2) { | ||
k = (help1 + help2).toString() | ||
help1 = help2 | ||
help2 = k.toInt() | ||
} else k = "1" | ||
k = fib(i).toString() | ||
count += k.length | ||
if (count >= n) break | ||
|
||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -263,7 +263,7 @@ fun decimal(digits: List<Int>, base: Int): Int = | |
* Например: str = "13c", base = 14 -> 250 | ||
*/ | ||
fun symbolToNumber(str: Char): Int = | ||
if (str <= '9') str - '9' + 9 | ||
if (str <= '9') str - '0' | ||
else str - 'a' + 10 | ||
|
||
fun decimalFromString(str: String, base: Int): Int = decimal(str.map { symbolToNumber(it) }, base) | ||
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. 👍 👍 👍 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. 👍 |
||
|
@@ -280,8 +280,8 @@ fun decimalFromString(str: String, base: Int): Int = decimal(str.map { symbolToN | |
fun roman(n: Int): String { | ||
var number = n | ||
var res = "" | ||
|
||
val listAll = listOf(Pair("I", 1), Pair("IV", 4), Pair("V", 5), Pair("IX", 9), Pair("X", 10), Pair("XL", 40), Pair("L", 50), | ||
Pair("XC", 90), Pair("C", 100), Pair("CD", 400), Pair("D", 500), Pair("CM", 900), Pair("M", 1000)) | ||
val listAll = listOf("I" to 1, "IV" to 4, "V" to 5, "IX" to 9, "X" to 10, "XL" to 40, "L" to 50, | ||
"XC" to 90, "C" to 100, "CD" to 400, "D" to 500, "CM" to 900, "M" to 1000) | ||
if (number <= 0) return "" | ||
else { | ||
|
||
while (number > 0) { | ||
|
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.
Зачем данная проверка? Можно ли обойтись без нее, если изменить, например, диапазон итерирования цикла?