Skip to content

Instantly share code, notes, and snippets.

@rabestro
Created May 11, 2020 09:13
Show Gist options
  • Save rabestro/d03c0fea64105de21e1d208cdafd9db4 to your computer and use it in GitHub Desktop.
Save rabestro/d03c0fea64105de21e1d208cdafd9db4 to your computer and use it in GitHub Desktop.
For loop and ranges → The roots of equation

There are numbers a,b,c,d. Output in ascending order all the integers between 0 and 1000 (inclusively), which are the roots of the equation

a⋅x^3 + b⋅x^2 + c⋅x + d = 0

If a specified interval does not contain any roots of the equation, do not output anything.

fun main(args: Array<String>) {
val scanner = java.util.Scanner(System.`in`)
val (a, b, c, d) = Array(4) { scanner.nextLong() }
(0..1000)
.filter { 0L == d + (c + (b + a * it) * it) * it }
.forEach { println(it) }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment