Last active
October 21, 2022 21:55
-
-
Save Tokiyomi/e5543bad62f958e2a41247e20d7a52e4 to your computer and use it in GitHub Desktop.
Generate a list of powers of two in Kotlin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Kotlin solution | |
fun powers_of_two(N:Int):MutableList<Long> { | |
/* | |
This function generates a tactical N set of numbers made of powers of 2 | |
and the remaining N-30 numbers will be the last N numbers before 10**9 inclusive. | |
As N is always 100, this fuction is always performed without problems | |
*/ | |
var A = mutableListOf<Long>() // Open empty list | |
// Append first 30 powers of two | |
for (t in 0..29) { | |
val base = 2 | |
var exponent = t | |
var result = Math.pow(base.toDouble(), exponent.toDouble()) | |
A.add(result.toLong()) | |
} | |
// Append the other 70 garbage numbers (in this case the last 70 allowed) | |
for (t in 0..N-31) { | |
val base = 10 | |
val exponent = 9 | |
val max_value = Math.pow(base.toDouble(), exponent.toDouble()) | |
A.add(max_value.toLong()-t) | |
} | |
return A | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment