All Downloads are FREE. Search and download functionalities are using the official Maven repository.

g1401_1500.s1447_simplified_fractions.Solution.kt Maven / Gradle / Ivy

There is a newer version: 1.28
Show newest version
package g1401_1500.s1447_simplified_fractions

// #Medium #String #Math #Number_Theory #2023_06_07_Time_338_ms_(100.00%)_Space_38_MB_(100.00%)

class Solution {
    fun simplifiedFractions(n: Int): List {
        val result: MutableList = ArrayList()
        if (n == 1) {
            return result
        }
        val str = StringBuilder()
        for (denom in 2..n) {
            for (num in 1 until denom) {
                if (checkGCD(num, denom) == 1) {
                    result.add(str.append(num).append("/").append(denom).toString())
                }
                str.setLength(0)
            }
        }
        return result
    }

    private fun checkGCD(a: Int, b: Int): Int {
        if (a < b) {
            return checkGCD(b, a)
        }
        return if (a == b || a % b == 0 || b == 1) {
            b
        } else checkGCD(a % b, b)
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy