import kotlin.math.floor

/**
 * Number letter counts
 *
 * If the numbers 1 to 5 are written out in words: one, two, three, four, five, then there are 3 + 3 + 5 + 4 + 4 = 19
 * letters used in total.
 *
 * If all the numbers from 1 to 1000 (one thousand) inclusive were written out in words, how many letters would be used?
 *
 * NOTE: Do not count spaces or hyphens. For example, 342 (three hundred and forty-two) contains 23 letters and 115 (one
 * hundred and fifteen) contains 20 letters. The use of "and" when writing out numbers is in compliance with British
 * usage.
 *
 * https://projecteuler.net/problem=17
 */

fun main() {
    println(numberLetterCount(115))
    println(numberLetterCount(342))
    println(sumNumberLetterCounts(5))
    println(sumNumberLetterCounts(1_000))
}

private fun sumNumberLetterCounts(max: Int): Int = (1..max).sumOf(::numberLetterCount)

private fun numberLetterCount(number: Int): Int {
    if (number < 100)
        return below100(number)

    var res = 0;
    val h = floor(number / 100f) % 10
    val t = floor(number / 1000f).toInt()
    val s = number % 100

    if (number > 999) {
        res += below100(t) + "thousand".length
    }
    if (h != 0f) {
        res += proper[h.toInt()] + "hundred".length
    }
    if (s != 0) {
        res += "and".length + below100(s)
    }
    return res
}

// Returns the length of the numbers between 0 and 99
private fun below100(n: Int): Int = if (n < 20) proper[n] else tenth[n / 10 - 2] + proper[n % 10]

// proper nouns
private val proper = arrayOf(
    0,
    "one".length,
    "two".length,
    "three".length,
    "four".length,
    "five".length,
    "six".length,
    "seven".length,
    "eight".length,
    "nine".length,
    "ten".length,
    "eleven".length,
    "twelve".length,
    "thirteen".length,
    "fourteen".length,
    "fifteen".length,
    "sixteen".length,
    "seventeen".length,
    "eighteen".length,
    "nineteen".length,
)

// tenth prefixes
private val tenth = arrayOf(
    "twenty".length,
    "thirty".length,
    "forty".length,
    "fifty".length,
    "sixty".length,
    "seventy".length,
    "eighty".length,
    "ninety".length,
)

Compiled from "Problem17.kt"
public final class Problem17Kt {
  private static final java.lang.Integer[] proper;

  private static final java.lang.Integer[] tenth;

  public static final void main();
    Code:
       0: bipush        115
       2: invokestatic  #10                 // Method numberLetterCount:(I)I
       5: istore_0
       6: getstatic     #16                 // Field java/lang/System.out:Ljava/io/PrintStream;
       9: iload_0
      10: invokevirtual #22                 // Method java/io/PrintStream.println:(I)V
      13: sipush        342
      16: invokestatic  #10                 // Method numberLetterCount:(I)I
      19: istore_0
      20: getstatic     #16                 // Field java/lang/System.out:Ljava/io/PrintStream;
      23: iload_0
      24: invokevirtual #22                 // Method java/io/PrintStream.println:(I)V
      27: iconst_5
      28: invokestatic  #25                 // Method sumNumberLetterCounts:(I)I
      31: istore_0
      32: getstatic     #16                 // Field java/lang/System.out:Ljava/io/PrintStream;
      35: iload_0
      36: invokevirtual #22                 // Method java/io/PrintStream.println:(I)V
      39: sipush        1000
      42: invokestatic  #25                 // Method sumNumberLetterCounts:(I)I
      45: istore_0
      46: getstatic     #16                 // Field java/lang/System.out:Ljava/io/PrintStream;
      49: iload_0
      50: invokevirtual #22                 // Method java/io/PrintStream.println:(I)V
      53: return

  private static final int sumNumberLetterCounts(int);
    Code:
       0: new           #27                 // class kotlin/ranges/IntRange
       3: dup
       4: iconst_1
       5: iload_0
       6: invokespecial #31                 // Method kotlin/ranges/IntRange."<init>":(II)V
       9: checkcast     #33                 // class java/lang/Iterable
      12: astore_1
      13: iconst_0
      14: istore_2
      15: aload_1
      16: invokeinterface #37,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
      21: astore_3
      22: aload_3
      23: invokeinterface #43,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
      28: ifeq          66
      31: aload_3
      32: checkcast     #45                 // class kotlin/collections/IntIterator
      35: invokevirtual #49                 // Method kotlin/collections/IntIterator.nextInt:()I
      38: istore        4
      40: iload_2
      41: iload         4
      43: istore        5
      45: istore        7
      47: iconst_0
      48: istore        6
      50: iload         5
      52: invokestatic  #10                 // Method numberLetterCount:(I)I
      55: istore        8
      57: iload         7
      59: iload         8
      61: iadd
      62: istore_2
      63: goto          22
      66: iload_2
      67: ireturn

  private static final int numberLetterCount(int);
    Code:
       0: iload_0
       1: bipush        100
       3: if_icmpge     11
       6: iload_0
       7: invokestatic  #56                 // Method below100:(I)I
      10: ireturn
      11: iconst_0
      12: istore_1
      13: iload_0
      14: i2f
      15: ldc           #57                 // float 100.0f
      17: fdiv
      18: f2d
      19: invokestatic  #63                 // Method java/lang/Math.floor:(D)D
      22: d2f
      23: bipush        10
      25: i2f
      26: frem
      27: fstore_2
      28: iload_0
      29: i2f
      30: ldc           #64                 // float 1000.0f
      32: fdiv
      33: f2d
      34: invokestatic  #63                 // Method java/lang/Math.floor:(D)D
      37: d2f
      38: f2i
      39: istore_3
      40: iload_0
      41: bipush        100
      43: irem
      44: istore        4
      46: iload_0
      47: sipush        999
      50: if_icmple     63
      53: iload_1
      54: iload_3
      55: invokestatic  #56                 // Method below100:(I)I
      58: bipush        8
      60: iadd
      61: iadd
      62: istore_1
      63: fload_2
      64: fconst_0
      65: fcmpg
      66: ifne          73
      69: iconst_1
      70: goto          74
      73: iconst_0
      74: ifne          92
      77: iload_1
      78: getstatic     #68                 // Field proper:[Ljava/lang/Integer;
      81: fload_2
      82: f2i
      83: aaload
      84: invokevirtual #73                 // Method java/lang/Integer.intValue:()I
      87: bipush        7
      89: iadd
      90: iadd
      91: istore_1
      92: iload         4
      94: ifeq          107
      97: iload_1
      98: iconst_3
      99: iload         4
     101: invokestatic  #56                 // Method below100:(I)I
     104: iadd
     105: iadd
     106: istore_1
     107: iload_1
     108: ireturn

  private static final int below100(int);
    Code:
       0: iload_0
       1: bipush        20
       3: if_icmpge     17
       6: getstatic     #68                 // Field proper:[Ljava/lang/Integer;
       9: iload_0
      10: aaload
      11: invokevirtual #73                 // Method java/lang/Integer.intValue:()I
      14: goto          42
      17: getstatic     #82                 // Field tenth:[Ljava/lang/Integer;
      20: iload_0
      21: bipush        10
      23: idiv
      24: iconst_2
      25: isub
      26: aaload
      27: invokevirtual #73                 // Method java/lang/Integer.intValue:()I
      30: getstatic     #68                 // Field proper:[Ljava/lang/Integer;
      33: iload_0
      34: bipush        10
      36: irem
      37: aaload
      38: invokevirtual #73                 // Method java/lang/Integer.intValue:()I
      41: iadd
      42: ireturn

  public static void main(java.lang.String[]);
    Code:
       0: invokestatic  #86                 // Method main:()V
       3: return

  static {};
    Code:
       0: bipush        20
       2: anewarray     #70                 // class java/lang/Integer
       5: astore_0
       6: aload_0
       7: iconst_0
       8: iconst_0
       9: invokestatic  #93                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      12: aastore
      13: aload_0
      14: iconst_1
      15: iconst_3
      16: invokestatic  #93                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      19: aastore
      20: aload_0
      21: iconst_2
      22: iconst_3
      23: invokestatic  #93                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      26: aastore
      27: aload_0
      28: iconst_3
      29: iconst_5
      30: invokestatic  #93                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      33: aastore
      34: aload_0
      35: iconst_4
      36: iconst_4
      37: invokestatic  #93                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      40: aastore
      41: aload_0
      42: iconst_5
      43: iconst_4
      44: invokestatic  #93                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      47: aastore
      48: aload_0
      49: bipush        6
      51: iconst_3
      52: invokestatic  #93                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      55: aastore
      56: aload_0
      57: bipush        7
      59: iconst_5
      60: invokestatic  #93                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      63: aastore
      64: aload_0
      65: bipush        8
      67: iconst_5
      68: invokestatic  #93                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      71: aastore
      72: aload_0
      73: bipush        9
      75: iconst_4
      76: invokestatic  #93                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      79: aastore
      80: aload_0
      81: bipush        10
      83: iconst_3
      84: invokestatic  #93                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      87: aastore
      88: aload_0
      89: bipush        11
      91: bipush        6
      93: invokestatic  #93                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      96: aastore
      97: aload_0
      98: bipush        12
     100: bipush        6
     102: invokestatic  #93                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     105: aastore
     106: aload_0
     107: bipush        13
     109: bipush        8
     111: invokestatic  #93                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     114: aastore
     115: aload_0
     116: bipush        14
     118: bipush        8
     120: invokestatic  #93                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     123: aastore
     124: aload_0
     125: bipush        15
     127: bipush        7
     129: invokestatic  #93                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     132: aastore
     133: aload_0
     134: bipush        16
     136: bipush        7
     138: invokestatic  #93                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     141: aastore
     142: aload_0
     143: bipush        17
     145: bipush        9
     147: invokestatic  #93                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     150: aastore
     151: aload_0
     152: bipush        18
     154: bipush        8
     156: invokestatic  #93                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     159: aastore
     160: aload_0
     161: bipush        19
     163: bipush        8
     165: invokestatic  #93                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     168: aastore
     169: aload_0
     170: putstatic     #68                 // Field proper:[Ljava/lang/Integer;
     173: bipush        8
     175: anewarray     #70                 // class java/lang/Integer
     178: astore_0
     179: aload_0
     180: iconst_0
     181: bipush        6
     183: invokestatic  #93                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     186: aastore
     187: aload_0
     188: iconst_1
     189: bipush        6
     191: invokestatic  #93                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     194: aastore
     195: aload_0
     196: iconst_2
     197: iconst_5
     198: invokestatic  #93                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     201: aastore
     202: aload_0
     203: iconst_3
     204: iconst_5
     205: invokestatic  #93                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     208: aastore
     209: aload_0
     210: iconst_4
     211: iconst_5
     212: invokestatic  #93                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     215: aastore
     216: aload_0
     217: iconst_5
     218: bipush        7
     220: invokestatic  #93                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     223: aastore
     224: aload_0
     225: bipush        6
     227: bipush        6
     229: invokestatic  #93                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     232: aastore
     233: aload_0
     234: bipush        7
     236: bipush        6
     238: invokestatic  #93                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     241: aastore
     242: aload_0
     243: putstatic     #82                 // Field tenth:[Ljava/lang/Integer;
     246: return
}


import kotlin.math.pow

/**
 * Distinct powers
 *
 * Consider all integer combinations of ab for 2 ≤ a ≤ 5 and 2 ≤ b ≤ 5:
 *
 * 2^2=4, 2^3=8, 2^4=16, 2^5=32
 * 3^2=9, 3^3=27, 3^4=81, 3^5=243
 * 4^2=16, 4^3=64, 4^4=256, 4^5=1024
 * 5^2=25, 5^3=125, 5^4=625, 55^=3125
 * If they are then placed in numerical order, with any repeats removed, we get the following sequence of 15 distinct
 * terms:
 *
 * 4, 8, 9, 16, 25, 27, 32, 64, 81, 125, 243, 256, 625, 1024, 3125
 *
 * How many distinct terms are in the sequence generated by ab for 2 ≤ a ≤ 100 and 2 ≤ b ≤ 100?
 *
 * https://projecteuler.net/problem=29
 */
fun main() {
    println(distinctPowers(limit = 5))
    println(distinctPowers(limit = 100))
}

private fun distinctPowers(limit: Int): Int = sequence {
    for (a in 2..limit) {
        for (b in 2..limit) {
            yield(a.toDouble().pow(b))
        }
    }
}.distinct().count()

Compiled from "Problem29.kt"
public final class Problem29Kt {
  public static final void main();
    Code:
       0: iconst_5
       1: invokestatic  #10                 // Method distinctPowers:(I)I
       4: istore_0
       5: getstatic     #16                 // Field java/lang/System.out:Ljava/io/PrintStream;
       8: iload_0
       9: invokevirtual #22                 // Method java/io/PrintStream.println:(I)V
      12: bipush        100
      14: invokestatic  #10                 // Method distinctPowers:(I)I
      17: istore_0
      18: getstatic     #16                 // Field java/lang/System.out:Ljava/io/PrintStream;
      21: iload_0
      22: invokevirtual #22                 // Method java/io/PrintStream.println:(I)V
      25: return

  private static final int distinctPowers(int);
    Code:
       0: new           #24                 // class Problem29Kt$distinctPowers$1
       3: dup
       4: iload_0
       5: aconst_null
       6: invokespecial #28                 // Method Problem29Kt$distinctPowers$1."<init>":(ILkotlin/coroutines/Continuation;)V
       9: checkcast     #30                 // class kotlin/jvm/functions/Function2
      12: invokestatic  #36                 // Method kotlin/sequences/SequencesKt.sequence:(Lkotlin/jvm/functions/Function2;)Lkotlin/sequences/Sequence;
      15: invokestatic  #40                 // Method kotlin/sequences/SequencesKt.distinct:(Lkotlin/sequences/Sequence;)Lkotlin/sequences/Sequence;
      18: invokestatic  #44                 // Method kotlin/sequences/SequencesKt.count:(Lkotlin/sequences/Sequence;)I
      21: ireturn

  public static void main(java.lang.String[]);
    Code:
       0: invokestatic  #49                 // Method main:()V
       3: return
}

Compiled from "Problem29.kt"
final class Problem29Kt$distinctPowers$1 extends kotlin.coroutines.jvm.internal.RestrictedSuspendLambda implements kotlin.jvm.functions.Function2<kotlin.sequences.SequenceScope<? super java.lang.Double>, kotlin.coroutines.Continuation<? super kotlin.Unit>, java.lang.Object> {
  int I$0;

  int I$1;

  int label;

  private java.lang.Object L$0;

  final int $limit;

  Problem29Kt$distinctPowers$1(int, kotlin.coroutines.Continuation<? super Problem29Kt$distinctPowers$1>);
    Code:
       0: aload_0
       1: iload_1
       2: putfield      #14                 // Field $limit:I
       5: aload_0
       6: iconst_2
       7: aload_2
       8: invokespecial #16                 // Method kotlin/coroutines/jvm/internal/RestrictedSuspendLambda."<init>":(ILkotlin/coroutines/Continuation;)V
      11: return

  public final java.lang.Object invokeSuspend(java.lang.Object);
    Code:
       0: invokestatic  #45                 // Method kotlin/coroutines/intrinsics/IntrinsicsKt.getCOROUTINE_SUSPENDED:()Ljava/lang/Object;
       3: astore        5
       5: aload_0
       6: getfield      #48                 // Field label:I
       9: tableswitch   { // 0 to 1
                     0: 32
                     1: 115
               default: 173
          }
      32: aload_1
      33: invokestatic  #54                 // Method kotlin/ResultKt.throwOnFailure:(Ljava/lang/Object;)V
      36: aload_0
      37: getfield      #57                 // Field L$0:Ljava/lang/Object;
      40: checkcast     #59                 // class kotlin/sequences/SequenceScope
      43: astore_2
      44: iconst_2
      45: istore_3
      46: iload_3
      47: aload_0
      48: getfield      #14                 // Field $limit:I
      51: if_icmpgt     169
      54: iconst_2
      55: istore        4
      57: iload         4
      59: aload_0
      60: getfield      #14                 // Field $limit:I
      63: if_icmpgt     155
      66: aload_2
      67: iload_3
      68: i2d
      69: iload         4
      71: i2d
      72: invokestatic  #65                 // Method java/lang/Math.pow:(DD)D
      75: invokestatic  #71                 // Method kotlin/coroutines/jvm/internal/Boxing.boxDouble:(D)Ljava/lang/Double;
      78: aload_0
      79: checkcast     #73                 // class kotlin/coroutines/Continuation
      82: aload_0
      83: aload_2
      84: putfield      #57                 // Field L$0:Ljava/lang/Object;
      87: aload_0
      88: iload_3
      89: putfield      #75                 // Field I$0:I
      92: aload_0
      93: iload         4
      95: putfield      #77                 // Field I$1:I
      98: aload_0
      99: iconst_1
     100: putfield      #48                 // Field label:I
     103: invokevirtual #81                 // Method kotlin/sequences/SequenceScope.yield:(Ljava/lang/Object;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
     106: dup
     107: aload         5
     109: if_acmpne     139
     112: aload         5
     114: areturn
     115: aload_0
     116: getfield      #77                 // Field I$1:I
     119: istore        4
     121: aload_0
     122: getfield      #75                 // Field I$0:I
     125: istore_3
     126: aload_0
     127: getfield      #57                 // Field L$0:Ljava/lang/Object;
     130: checkcast     #59                 // class kotlin/sequences/SequenceScope
     133: astore_2
     134: aload_1
     135: invokestatic  #54                 // Method kotlin/ResultKt.throwOnFailure:(Ljava/lang/Object;)V
     138: aload_1
     139: pop
     140: iload         4
     142: aload_0
     143: getfield      #14                 // Field $limit:I
     146: if_icmpeq     155
     149: iinc          4, 1
     152: goto          66
     155: iload_3
     156: aload_0
     157: getfield      #14                 // Field $limit:I
     160: if_icmpeq     169
     163: iinc          3, 1
     166: goto          54
     169: getstatic     #87                 // Field kotlin/Unit.INSTANCE:Lkotlin/Unit;
     172: areturn
     173: new           #89                 // class java/lang/IllegalStateException
     176: dup
     177: ldc           #91                 // String call to \'resume\' before \'invoke\' with coroutine
     179: invokespecial #94                 // Method java/lang/IllegalStateException."<init>":(Ljava/lang/String;)V
     182: athrow

  public final kotlin.coroutines.Continuation<kotlin.Unit> create(java.lang.Object, kotlin.coroutines.Continuation<?>);
    Code:
       0: new           #2                  // class Problem29Kt$distinctPowers$1
       3: dup
       4: aload_0
       5: getfield      #14                 // Field $limit:I
       8: aload_2
       9: invokespecial #102                // Method "<init>":(ILkotlin/coroutines/Continuation;)V
      12: astore_3
      13: aload_3
      14: aload_1
      15: putfield      #57                 // Field L$0:Ljava/lang/Object;
      18: aload_3
      19: checkcast     #73                 // class kotlin/coroutines/Continuation
      22: areturn

  public final java.lang.Object invoke(kotlin.sequences.SequenceScope<? super java.lang.Double>, kotlin.coroutines.Continuation<? super kotlin.Unit>);
    Code:
       0: aload_0
       1: aload_1
       2: aload_2
       3: invokevirtual #108                // Method create:(Ljava/lang/Object;Lkotlin/coroutines/Continuation;)Lkotlin/coroutines/Continuation;
       6: checkcast     #2                  // class Problem29Kt$distinctPowers$1
       9: getstatic     #87                 // Field kotlin/Unit.INSTANCE:Lkotlin/Unit;
      12: invokevirtual #110                // Method invokeSuspend:(Ljava/lang/Object;)Ljava/lang/Object;
      15: areturn

  public java.lang.Object invoke(java.lang.Object, java.lang.Object);
    Code:
       0: aload_0
       1: aload_1
       2: checkcast     #59                 // class kotlin/sequences/SequenceScope
       5: aload_2
       6: checkcast     #73                 // class kotlin/coroutines/Continuation
       9: invokevirtual #115                // Method invoke:(Lkotlin/sequences/SequenceScope;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
      12: areturn
}


import java.math.BigInteger

/**
 * Factorial digit sum
 *
 * n! means n × (n − 1) × ... × 3 × 2 × 1
 *
 * For example, 10! = 10 × 9 × ... × 3 × 2 × 1 = 3628800,
 * and the sum of the digits in the number 10! is 3 + 6 + 2 + 8 + 8 + 0 + 0 = 27.
 *
 * Find the sum of the digits in the number 100!
 *
 * https://projecteuler.net/problem=20
 */
fun main() {
    println(factorialDigitSum(10))
    println(factorialDigitSum(100))
}

private fun factorialDigitSum(n: Int): Int {
    val result = mutableListOf<Int>().apply {
        var num = n
        while (num != 0) {
            add(num % 10)
            num /= 10
        }
    }

    for (factor in (n - 1) downTo 2) {
        var acc = 0
        for ((i, num) in result.withIndex()) {
            val res = (num * factor) + acc
            result[i] = res % 10
            acc = res / 10
        }

        while (acc != 0) {
            result.add(acc % 10)
            acc /= 10
        }
    }
    return result.sum()
}

Compiled from "Problem20.kt"
public final class Problem20Kt {
  public static final void main();
    Code:
       0: bipush        10
       2: invokestatic  #10                 // Method factorialDigitSum:(I)I
       5: istore_0
       6: getstatic     #16                 // Field java/lang/System.out:Ljava/io/PrintStream;
       9: iload_0
      10: invokevirtual #22                 // Method java/io/PrintStream.println:(I)V
      13: bipush        100
      15: invokestatic  #10                 // Method factorialDigitSum:(I)I
      18: istore_0
      19: getstatic     #16                 // Field java/lang/System.out:Ljava/io/PrintStream;
      22: iload_0
      23: invokevirtual #22                 // Method java/io/PrintStream.println:(I)V
      26: return

  private static final int factorialDigitSum(int);
    Code:
       0: new           #24                 // class java/util/ArrayList
       3: dup
       4: invokespecial #27                 // Method java/util/ArrayList."<init>":()V
       7: checkcast     #29                 // class java/util/List
      10: astore_2
      11: aload_2
      12: astore_3
      13: iconst_0
      14: istore        4
      16: iload_0
      17: istore        5
      19: iload         5
      21: ifeq          49
      24: aload_3
      25: iload         5
      27: bipush        10
      29: irem
      30: invokestatic  #35                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      33: invokeinterface #39,  2           // InterfaceMethod java/util/List.add:(Ljava/lang/Object;)Z
      38: pop
      39: iload         5
      41: bipush        10
      43: idiv
      44: istore        5
      46: goto          19
      49: nop
      50: aload_2
      51: astore_1
      52: iload_0
      53: iconst_1
      54: isub
      55: istore_2
      56: iconst_1
      57: iload_2
      58: if_icmpge     178
      61: iconst_0
      62: istore_3
      63: aload_1
      64: checkcast     #41                 // class java/lang/Iterable
      67: invokeinterface #45,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
      72: astore        4
      74: iconst_0
      75: istore        5
      77: aload         4
      79: invokeinterface #51,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
      84: ifeq          146
      87: iload         5
      89: istore        6
      91: iload         5
      93: iconst_1
      94: iadd
      95: istore        5
      97: aload         4
      99: invokeinterface #55,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     104: checkcast     #57                 // class java/lang/Number
     107: invokevirtual #61                 // Method java/lang/Number.intValue:()I
     110: istore        7
     112: iload         7
     114: iload_2
     115: imul
     116: iload_3
     117: iadd
     118: istore        8
     120: aload_1
     121: iload         6
     123: iload         8
     125: bipush        10
     127: irem
     128: invokestatic  #35                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     131: invokeinterface #65,  3           // InterfaceMethod java/util/List.set:(ILjava/lang/Object;)Ljava/lang/Object;
     136: pop
     137: iload         8
     139: bipush        10
     141: idiv
     142: istore_3
     143: goto          77
     146: iload_3
     147: ifeq          172
     150: aload_1
     151: iload_3
     152: bipush        10
     154: irem
     155: invokestatic  #35                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     158: invokeinterface #39,  2           // InterfaceMethod java/util/List.add:(Ljava/lang/Object;)Z
     163: pop
     164: iload_3
     165: bipush        10
     167: idiv
     168: istore_3
     169: goto          146
     172: iinc          2, -1
     175: goto          56
     178: aload_1
     179: checkcast     #41                 // class java/lang/Iterable
     182: invokestatic  #71                 // Method kotlin/collections/CollectionsKt.sumOfInt:(Ljava/lang/Iterable;)I
     185: ireturn

  public static void main(java.lang.String[]);
    Code:
       0: invokestatic  #85                 // Method main:()V
       3: return
}


import kotlin.math.pow

/**
 * Digit fifth powers
 *
 * Surprisingly there are only three numbers that can be written as the sum of fourth powers of their digits:
 *
 * 1634 = 1^4 + 6^4 + 3^4 + 4^4
 * 8208 = 8^4 + 2^4 + 0^4 + 8^4
 * 9474 = 9^4 + 4^4 + 7^4 + 4^4
 *
 * As 1 = 14 is not a sum it is not included.
 *
 * The sum of these numbers is 1634 + 8208 + 9474 = 19316.
 *
 * Find the sum of all the numbers that can be written as the sum of fifth powers of their digits.
 *
 * https://projecteuler.net/problem=30
 */

fun main() {
    println(1634.isDigitPower(4))
    println(8208.isDigitPower(4))
    println(9474.isDigitPower(4))
    println(sumOfDigitPowersOf(4))
    println(sumOfDigitPowersOf(5))
}

private fun sumOfDigitPowersOf(n: Int): Int {
    var sum = 0
    val limit = 9.0.pow(n).times(n).toInt()
    for (i in 10..limit) {
        if (i.isDigitPower(n)) {
            sum += i
        }
    }
    return sum
}

private fun Int.isDigitPower(n: Int): Boolean {
    var sum = 0
    var num = this
    while (num != 0) {
        val d = num % 10
        sum += d.toDouble().pow(n).toInt()
        num /= 10
    }
    return sum == this
}

Compiled from "Problem30.kt"
public final class Problem30Kt {
  public static final void main();
    Code:
       0: sipush        1634
       3: iconst_4
       4: invokestatic  #10                 // Method isDigitPower:(II)Z
       7: istore_0
       8: getstatic     #16                 // Field java/lang/System.out:Ljava/io/PrintStream;
      11: iload_0
      12: invokevirtual #22                 // Method java/io/PrintStream.println:(Z)V
      15: sipush        8208
      18: iconst_4
      19: invokestatic  #10                 // Method isDigitPower:(II)Z
      22: istore_0
      23: getstatic     #16                 // Field java/lang/System.out:Ljava/io/PrintStream;
      26: iload_0
      27: invokevirtual #22                 // Method java/io/PrintStream.println:(Z)V
      30: sipush        9474
      33: iconst_4
      34: invokestatic  #10                 // Method isDigitPower:(II)Z
      37: istore_0
      38: getstatic     #16                 // Field java/lang/System.out:Ljava/io/PrintStream;
      41: iload_0
      42: invokevirtual #22                 // Method java/io/PrintStream.println:(Z)V
      45: iconst_4
      46: invokestatic  #26                 // Method sumOfDigitPowersOf:(I)I
      49: istore_0
      50: getstatic     #16                 // Field java/lang/System.out:Ljava/io/PrintStream;
      53: iload_0
      54: invokevirtual #29                 // Method java/io/PrintStream.println:(I)V
      57: iconst_5
      58: invokestatic  #26                 // Method sumOfDigitPowersOf:(I)I
      61: istore_0
      62: getstatic     #16                 // Field java/lang/System.out:Ljava/io/PrintStream;
      65: iload_0
      66: invokevirtual #29                 // Method java/io/PrintStream.println:(I)V
      69: return

  private static final int sumOfDigitPowersOf(int);
    Code:
       0: iconst_0
       1: istore_1
       2: ldc2_w        #30                 // double 9.0d
       5: iload_0
       6: i2d
       7: invokestatic  #37                 // Method java/lang/Math.pow:(DD)D
      10: iload_0
      11: i2d
      12: dmul
      13: d2i
      14: istore_2
      15: bipush        10
      17: istore_3
      18: iload_3
      19: iload_2
      20: if_icmpgt     46
      23: iload_3
      24: iload_0
      25: invokestatic  #10                 // Method isDigitPower:(II)Z
      28: ifeq          35
      31: iload_1
      32: iload_3
      33: iadd
      34: istore_1
      35: iload_3
      36: iload_2
      37: if_icmpeq     46
      40: iinc          3, 1
      43: goto          23
      46: iload_1
      47: ireturn

  private static final boolean isDigitPower(int, int);
    Code:
       0: iconst_0
       1: istore_2
       2: iload_0
       3: istore_3
       4: iload_3
       5: ifeq          34
       8: iload_3
       9: bipush        10
      11: irem
      12: istore        4
      14: iload_2
      15: iload         4
      17: i2d
      18: iload_1
      19: i2d
      20: invokestatic  #37                 // Method java/lang/Math.pow:(DD)D
      23: d2i
      24: iadd
      25: istore_2
      26: iload_3
      27: bipush        10
      29: idiv
      30: istore_3
      31: goto          4
      34: iload_2
      35: iload_0
      36: if_icmpne     43
      39: iconst_1
      40: goto          44
      43: iconst_0
      44: ireturn

  public static void main(java.lang.String[]);
    Code:
       0: invokestatic  #48                 // Method main:()V
       3: return
}


/**
 * Longest Collatz sequence
 *
 * The following iterative sequence is defined for the set of positive integers:
 *
 * n → n/2 (n is even)
 * n → 3n + 1 (n is odd)
 *
 * Using the rule above and starting with 13, we generate the following sequence:
 *
 * 13 → 40 → 20 → 10 → 5 → 16 → 8 → 4 → 2 → 1
 * It can be seen that this sequence (starting at 13 and finishing at 1) contains 10 terms. Although it has not been
 * proved yet (Collatz Problem), it is thought that all starting numbers finish at 1.
 *
 * Which starting number, under one million, produces the longest chain?
 *
 * NOTE: Once the chain starts the terms are allowed to go above one million.
 *
 * https://projecteuler.net/problem=14
 */
fun main() {
    println(longestCollatzSequence(1_000_000))
}

private fun longestCollatzSequence(max: Int): Int {
    var longest = 0
    var answer = 0
    for (n in (max / 2) until max) {
        val count = collatzSequenceSize(n)
        if (count > longest) {
            longest = count
            answer = n
        }
    }
    max.countLeadingZeroBits()
    return answer
}

private val cache = mutableMapOf(1 to 1)

private fun collatzSequenceSize(n: Int): Int {
    val cached = cache[n]
    if (cached != null) {
        return cached
    }

    val next = if (n % 2 == 0) {
        collatzSequenceSize(n / 2) + 1
    } else {
        collatzSequenceSize((3 * n + 1) / 2) + 2
    }
    if (cache.size <= 1 shl 30) {
        cache[n] = next
    }

    return next
}

Compiled from "Problem14.kt"
public final class Problem14Kt {
  private static final java.util.Map<java.lang.Integer, java.lang.Integer> cache;

  public static final void main();
    Code:
       0: ldc           #7                  // int 1000000
       2: invokestatic  #11                 // Method longestCollatzSequence:(I)I
       5: istore_0
       6: getstatic     #17                 // Field java/lang/System.out:Ljava/io/PrintStream;
       9: iload_0
      10: invokevirtual #23                 // Method java/io/PrintStream.println:(I)V
      13: return

  private static final int longestCollatzSequence(int);
    Code:
       0: iconst_0
       1: istore_1
       2: iconst_0
       3: istore_2
       4: iload_0
       5: iconst_2
       6: idiv
       7: istore_3
       8: iload_3
       9: iload_0
      10: if_icmpge     36
      13: iload_3
      14: invokestatic  #26                 // Method collatzSequenceSize:(I)I
      17: istore        4
      19: iload         4
      21: iload_1
      22: if_icmple     30
      25: iload         4
      27: istore_1
      28: iload_3
      29: istore_2
      30: iinc          3, 1
      33: goto          8
      36: iload_0
      37: invokestatic  #31                 // Method java/lang/Integer.numberOfLeadingZeros:(I)I
      40: pop
      41: iload_2
      42: ireturn

  private static final int collatzSequenceSize(int);
    Code:
       0: getstatic     #41                 // Field cache:Ljava/util/Map;
       3: iload_0
       4: invokestatic  #45                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
       7: invokeinterface #51,  2           // InterfaceMethod java/util/Map.get:(Ljava/lang/Object;)Ljava/lang/Object;
      12: checkcast     #28                 // class java/lang/Integer
      15: astore_1
      16: aload_1
      17: ifnull        25
      20: aload_1
      21: invokevirtual #55                 // Method java/lang/Integer.intValue:()I
      24: ireturn
      25: iload_0
      26: iconst_2
      27: irem
      28: ifne          42
      31: iload_0
      32: iconst_2
      33: idiv
      34: invokestatic  #26                 // Method collatzSequenceSize:(I)I
      37: iconst_1
      38: iadd
      39: goto          54
      42: iconst_3
      43: iload_0
      44: imul
      45: iconst_1
      46: iadd
      47: iconst_2
      48: idiv
      49: invokestatic  #26                 // Method collatzSequenceSize:(I)I
      52: iconst_2
      53: iadd
      54: istore_2
      55: getstatic     #41                 // Field cache:Ljava/util/Map;
      58: invokeinterface #58,  1           // InterfaceMethod java/util/Map.size:()I
      63: ldc           #59                 // int 1073741824
      65: if_icmpgt     91
      68: iload_0
      69: invokestatic  #45                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      72: astore_3
      73: iload_2
      74: invokestatic  #45                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      77: astore        4
      79: getstatic     #41                 // Field cache:Ljava/util/Map;
      82: aload_3
      83: aload         4
      85: invokeinterface #63,  3           // InterfaceMethod java/util/Map.put:(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;
      90: pop
      91: iload_2
      92: ireturn

  public static void main(java.lang.String[]);
    Code:
       0: invokestatic  #69                 // Method main:()V
       3: return

  static {};
    Code:
       0: iconst_1
       1: anewarray     #74                 // class kotlin/Pair
       4: astore_0
       5: aload_0
       6: iconst_0
       7: iconst_1
       8: invokestatic  #45                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      11: iconst_1
      12: invokestatic  #45                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      15: invokestatic  #80                 // Method kotlin/TuplesKt.to:(Ljava/lang/Object;Ljava/lang/Object;)Lkotlin/Pair;
      18: aastore
      19: aload_0
      20: invokestatic  #86                 // Method kotlin/collections/MapsKt.mutableMapOf:([Lkotlin/Pair;)Ljava/util/Map;
      23: putstatic     #41                 // Field cache:Ljava/util/Map;
      26: return
}


/**
 * Largest palindrome product
 *
 * A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is
 * 9009 = 91 × 99.
 *
 * Find the largest palindrome made from the product of two 3-digit numbers.
 *
 * https://projecteuler.net/problem=4
 */

fun main() {
    println(largestPalindrome())
}

fun largestPalindrome(): Int {
    var largestPalindrome = 0
    for (a in 999 downTo 100) {
        var b = 999
        while (b >= a) {
            val p = a * b
            if (p <= largestPalindrome) {
                break // since p is always going to be small
            }
            if (p.isPalindrome) {
                largestPalindrome = p
            }
            b--
        }
    }
    return largestPalindrome
}

val Int.isPalindrome: Boolean get() = this == reverse()

fun Int.reverse(): Int {
    var num = this
    var reversed = 0
    while (num > 0) {
        reversed = (10 * reversed) + (num % 10)
        num /= 10
    }
    return reversed
}

Compiled from "Problem4.kt"
public final class Problem4Kt {
  public static final void main();
    Code:
       0: invokestatic  #10                 // Method largestPalindrome:()I
       3: istore_0
       4: getstatic     #16                 // Field java/lang/System.out:Ljava/io/PrintStream;
       7: iload_0
       8: invokevirtual #22                 // Method java/io/PrintStream.println:(I)V
      11: return

  public static final int largestPalindrome();
    Code:
       0: iconst_0
       1: istore_0
       2: sipush        999
       5: istore_1
       6: bipush        99
       8: iload_1
       9: if_icmpge     54
      12: sipush        999
      15: istore_2
      16: iload_2
      17: iload_1
      18: if_icmplt     48
      21: iload_1
      22: iload_2
      23: imul
      24: istore_3
      25: iload_3
      26: iload_0
      27: if_icmpgt     33
      30: goto          48
      33: iload_3
      34: invokestatic  #26                 // Method isPalindrome:(I)Z
      37: ifeq          42
      40: iload_3
      41: istore_0
      42: iinc          2, -1
      45: goto          16
      48: iinc          1, -1
      51: goto          6
      54: iload_0
      55: ireturn

  public static final boolean isPalindrome(int);
    Code:
       0: iload_0
       1: iload_0
       2: invokestatic  #34                 // Method reverse:(I)I
       5: if_icmpne     12
       8: iconst_1
       9: goto          13
      12: iconst_0
      13: ireturn

  public static final int reverse(int);
    Code:
       0: iload_0
       1: istore_1
       2: iconst_0
       3: istore_2
       4: iload_1
       5: ifle          26
       8: bipush        10
      10: iload_2
      11: imul
      12: iload_1
      13: bipush        10
      15: irem
      16: iadd
      17: istore_2
      18: iload_1
      19: bipush        10
      21: idiv
      22: istore_1
      23: goto          4
      26: iload_2
      27: ireturn

  public static void main(java.lang.String[]);
    Code:
       0: invokestatic  #41                 // Method main:()V
       3: return
}


@file:Suppress("MagicNumber")

/**
 * Pandigital products
 *
 * We shall say that an n-digit number is pandigital if it makes use of all the digits 1 to n exactly once; for example,
 * the 5-digit number, 15234, is 1 through 5 pandigital.
 *
 * The product 7254 is unusual, as the identity, 39 × 186 = 7254, containing multiplicand, multiplier, and product is 1
 * through 9 pandigital.
 *
 * Find the sum of all products whose multiplicand/multiplier/product identity can be written as a 1 through 9
 * pandigital.
 *
 * HINT: Some products can be obtained in more than one way so be sure to only include it once in your sum.
 *
 * https://projecteuler.net/problem=32
 */
fun main() {
	println(isMultiplicationPandigital(multiplicand = 39, multiplier = 186, product = 7254))
	println(checkPandigitalsMultiplications())
}

private fun checkPandigitalsMultiplications(): Int = buildSet {
	for (i in 2 until 100) {
		for (j in (i + 1) until 2_000) {
			val p = i * j
			if (isMultiplicationPandigital(multiplicand = i, multiplier = j, product = p)) {
				add(p)
			}
		}
	}
}.sum()

private fun isMultiplicationPandigital(multiplicand: Int, multiplier: Int, product: Int): Boolean {
	check(multiplicand * multiplier == product)

	var digitsFlags = 0
	val digits = multiplicand.digits() + multiplier.digits() + product.digits()

	if (digits.size != 9) {
		return false
	}

	digits.forEach {
		digitsFlags = digitsFlags or (1 shl (it - 1))
	}

	return digitsFlags == 511
}

fun Int.digits(): List<Int> = buildList {
	var num = this@digits
	while (num != 0) {
		add(num % 10)
		num /= 10
	}
	reverse()
}

Compiled from "Problem32.kt"
public final class Problem32Kt {
  public static final void main();
    Code:
       0: bipush        39
       2: sipush        186
       5: sipush        7254
       8: invokestatic  #10                 // Method isMultiplicationPandigital:(III)Z
      11: istore_0
      12: getstatic     #16                 // Field java/lang/System.out:Ljava/io/PrintStream;
      15: iload_0
      16: invokevirtual #22                 // Method java/io/PrintStream.println:(Z)V
      19: invokestatic  #26                 // Method checkPandigitalsMultiplications:()I
      22: istore_0
      23: getstatic     #16                 // Field java/lang/System.out:Ljava/io/PrintStream;
      26: iload_0
      27: invokevirtual #29                 // Method java/io/PrintStream.println:(I)V
      30: return

  private static final int checkPandigitalsMultiplications();
    Code:
       0: invokestatic  #35                 // Method kotlin/collections/SetsKt.createSetBuilder:()Ljava/util/Set;
       3: astore_0
       4: aload_0
       5: astore_1
       6: iconst_0
       7: istore_2
       8: iconst_2
       9: istore_3
      10: iload_3
      11: bipush        100
      13: if_icmpge     70
      16: iload_3
      17: iconst_1
      18: iadd
      19: istore        4
      21: iload         4
      23: sipush        2000
      26: if_icmpge     64
      29: iload_3
      30: iload         4
      32: imul
      33: istore        5
      35: iload_3
      36: iload         4
      38: iload         5
      40: invokestatic  #10                 // Method isMultiplicationPandigital:(III)Z
      43: ifeq          58
      46: aload_1
      47: iload         5
      49: invokestatic  #41                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      52: invokeinterface #47,  2           // InterfaceMethod java/util/Set.add:(Ljava/lang/Object;)Z
      57: pop
      58: iinc          4, 1
      61: goto          21
      64: iinc          3, 1
      67: goto          10
      70: nop
      71: aload_0
      72: invokestatic  #51                 // Method kotlin/collections/SetsKt.build:(Ljava/util/Set;)Ljava/util/Set;
      75: checkcast     #53                 // class java/lang/Iterable
      78: invokestatic  #59                 // Method kotlin/collections/CollectionsKt.sumOfInt:(Ljava/lang/Iterable;)I
      81: ireturn

  private static final boolean isMultiplicationPandigital(int, int, int);
    Code:
       0: iload_0
       1: iload_1
       2: imul
       3: iload_2
       4: if_icmpne     11
       7: iconst_1
       8: goto          12
      11: iconst_0
      12: ifne          25
      15: new           #68                 // class java/lang/IllegalStateException
      18: dup
      19: ldc           #70                 // String Check failed.
      21: invokespecial #74                 // Method java/lang/IllegalStateException."<init>":(Ljava/lang/String;)V
      24: athrow
      25: iconst_0
      26: istore_3
      27: iload_0
      28: invokestatic  #78                 // Method digits:(I)Ljava/util/List;
      31: checkcast     #80                 // class java/util/Collection
      34: iload_1
      35: invokestatic  #78                 // Method digits:(I)Ljava/util/List;
      38: checkcast     #53                 // class java/lang/Iterable
      41: invokestatic  #84                 // Method kotlin/collections/CollectionsKt.plus:(Ljava/util/Collection;Ljava/lang/Iterable;)Ljava/util/List;
      44: checkcast     #80                 // class java/util/Collection
      47: iload_2
      48: invokestatic  #78                 // Method digits:(I)Ljava/util/List;
      51: checkcast     #53                 // class java/lang/Iterable
      54: invokestatic  #84                 // Method kotlin/collections/CollectionsKt.plus:(Ljava/util/Collection;Ljava/lang/Iterable;)Ljava/util/List;
      57: astore        4
      59: aload         4
      61: invokeinterface #89,  1           // InterfaceMethod java/util/List.size:()I
      66: bipush        9
      68: if_icmpeq     73
      71: iconst_0
      72: ireturn
      73: aload         4
      75: checkcast     #53                 // class java/lang/Iterable
      78: astore        5
      80: iconst_0
      81: istore        6
      83: aload         5
      85: invokeinterface #93,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
      90: astore        7
      92: aload         7
      94: invokeinterface #99,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
      99: ifeq          138
     102: aload         7
     104: invokeinterface #103,  1          // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     109: astore        8
     111: aload         8
     113: checkcast     #105                // class java/lang/Number
     116: invokevirtual #108                // Method java/lang/Number.intValue:()I
     119: istore        9
     121: iconst_0
     122: istore        10
     124: iload_3
     125: iconst_1
     126: iload         9
     128: iconst_1
     129: isub
     130: ishl
     131: ior
     132: istore_3
     133: nop
     134: nop
     135: goto          92
     138: nop
     139: iload_3
     140: sipush        511
     143: if_icmpne     150
     146: iconst_1
     147: goto          151
     150: iconst_0
     151: ireturn

  public static final java.util.List<java.lang.Integer> digits(int);
    Code:
       0: invokestatic  #126                // Method kotlin/collections/CollectionsKt.createListBuilder:()Ljava/util/List;
       3: astore_1
       4: aload_1
       5: astore_2
       6: iconst_0
       7: istore_3
       8: iload_0
       9: istore        4
      11: iload         4
      13: ifeq          41
      16: aload_2
      17: iload         4
      19: bipush        10
      21: irem
      22: invokestatic  #41                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      25: invokeinterface #127,  2          // InterfaceMethod java/util/List.add:(Ljava/lang/Object;)Z
      30: pop
      31: iload         4
      33: bipush        10
      35: idiv
      36: istore        4
      38: goto          11
      41: aload_2
      42: invokestatic  #131                // Method kotlin/collections/CollectionsKt.reverse:(Ljava/util/List;)V
      45: nop
      46: aload_1
      47: invokestatic  #134                // Method kotlin/collections/CollectionsKt.build:(Ljava/util/List;)Ljava/util/List;
      50: areturn

  public static void main(java.lang.String[]);
    Code:
       0: invokestatic  #141                // Method main:()V
       3: return
}


import kotlin.math.floor
import kotlin.math.log10
import kotlin.math.pow
import kotlin.math.sqrt

/**
 * Smallest multiple
 *
 * 2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.
 *
 * What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?
 *
 * https://projecteuler.net/problem=5
 */
fun main() {
    println(smallestNumberDivisibleBy(20))
}

// Initial answer
/*fun smallestNumberDivisibleBy(k : Int): Long {
    for (n in (k..Long.MAX_VALUE)) {
        if ((2..k).all { n % it == 0L }) {
            return n
        }
    }
    return 0
}*/

private fun smallestNumberDivisibleBy(k: Int): Long {
    val p = primes(k)
    val a = Array(p.size) { 0 }
    val limit = sqrt(k.toDouble())
    var n = 1L
    var check = true
    for (i in 1 until p.size) {
        a[i] = 1
        if (check) {
            if (p[i] <= limit) {
                a[i] = floor(log10(k.toDouble())/ log10(p[i].toDouble())).toInt()
            } else {
                check = false
            }
        }
        n *= p[i].toDouble().pow(a[i]).toLong()
    }
    return n
}

// Generating a List of Primes: The Sieve of Eratosthenes
fun primes(n: Int): List<Int> {
    val flags = Array(n) { true }
    var prime = 2

    while (prime <= sqrt(n.toDouble())) {
        flags.crossOff(prime)
        prime = flags.indexOfFirst(from = prime + 1) { it }
    }

    return flags.mapIndexedNotNull { index, isPrime -> if (isPrime) index else null }
}

private fun Array<Boolean>.crossOff(prime: Int) {
    for (i in (prime * prime) until size step prime) {
        this[i] = false
    }
}

inline fun <T> Array<out T>.indexOfFirst(from: Int, predicate: (T) -> Boolean): Int {
    for (index in from until size) {
        if (predicate(this[index])) {
            return index
        }
    }
    return -1
}

Compiled from "Problem5.kt"
public final class Problem5Kt {
  public static final void main();
    Code:
       0: bipush        20
       2: invokestatic  #10                 // Method smallestNumberDivisibleBy:(I)J
       5: lstore_0
       6: getstatic     #16                 // Field java/lang/System.out:Ljava/io/PrintStream;
       9: lload_0
      10: invokevirtual #22                 // Method java/io/PrintStream.println:(J)V
      13: return

  private static final long smallestNumberDivisibleBy(int);
    Code:
       0: iload_0
       1: invokestatic  #26                 // Method primes:(I)Ljava/util/List;
       4: astore_1
       5: iconst_0
       6: istore_3
       7: aload_1
       8: invokeinterface #32,  1           // InterfaceMethod java/util/List.size:()I
      13: istore        4
      15: iload         4
      17: anewarray     #34                 // class java/lang/Integer
      20: astore        5
      22: iload_3
      23: iload         4
      25: if_icmpge     46
      28: iload_3
      29: istore        6
      31: aload         5
      33: iload         6
      35: iconst_0
      36: invokestatic  #38                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      39: aastore
      40: iinc          3, 1
      43: goto          22
      46: aload         5
      48: astore_2
      49: iload_0
      50: i2d
      51: invokestatic  #44                 // Method java/lang/Math.sqrt:(D)D
      54: dstore_3
      55: lconst_1
      56: lstore        5
      58: iconst_1
      59: istore        7
      61: iconst_1
      62: istore        8
      64: aload_1
      65: invokeinterface #32,  1           // InterfaceMethod java/util/List.size:()I
      70: istore        9
      72: iload         8
      74: iload         9
      76: if_icmpge     191
      79: aload_2
      80: iload         8
      82: iconst_1
      83: invokestatic  #38                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      86: aastore
      87: iload         7
      89: ifeq          153
      92: aload_1
      93: iload         8
      95: invokeinterface #48,  2           // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     100: checkcast     #50                 // class java/lang/Number
     103: invokevirtual #53                 // Method java/lang/Number.intValue:()I
     106: i2d
     107: dload_3
     108: dcmpg
     109: ifgt          150
     112: aload_2
     113: iload         8
     115: iload_0
     116: i2d
     117: invokestatic  #56                 // Method java/lang/Math.log10:(D)D
     120: aload_1
     121: iload         8
     123: invokeinterface #48,  2           // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     128: checkcast     #50                 // class java/lang/Number
     131: invokevirtual #53                 // Method java/lang/Number.intValue:()I
     134: i2d
     135: invokestatic  #56                 // Method java/lang/Math.log10:(D)D
     138: ddiv
     139: invokestatic  #59                 // Method java/lang/Math.floor:(D)D
     142: d2i
     143: invokestatic  #38                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     146: aastore
     147: goto          153
     150: iconst_0
     151: istore        7
     153: lload         5
     155: aload_1
     156: iload         8
     158: invokeinterface #48,  2           // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     163: checkcast     #50                 // class java/lang/Number
     166: invokevirtual #53                 // Method java/lang/Number.intValue:()I
     169: i2d
     170: aload_2
     171: iload         8
     173: aaload
     174: invokevirtual #60                 // Method java/lang/Integer.intValue:()I
     177: i2d
     178: invokestatic  #64                 // Method java/lang/Math.pow:(DD)D
     181: d2l
     182: lmul
     183: lstore        5
     185: iinc          8, 1
     188: goto          72
     191: lload         5
     193: lreturn

  public static final java.util.List<java.lang.Integer> primes(int);
    Code:
       0: iconst_0
       1: istore_2
       2: iload_0
       3: anewarray     #82                 // class java/lang/Boolean
       6: astore_3
       7: iload_2
       8: iload_0
       9: if_icmpge     29
      12: iload_2
      13: istore        4
      15: aload_3
      16: iload         4
      18: iconst_1
      19: invokestatic  #85                 // Method java/lang/Boolean.valueOf:(Z)Ljava/lang/Boolean;
      22: aastore
      23: iinc          2, 1
      26: goto          7
      29: aload_3
      30: astore_1
      31: iconst_2
      32: istore_2
      33: iload_2
      34: i2d
      35: iload_0
      36: i2d
      37: invokestatic  #44                 // Method java/lang/Math.sqrt:(D)D
      40: dcmpg
      41: ifgt          107
      44: aload_1
      45: iload_2
      46: invokestatic  #89                 // Method crossOff:([Ljava/lang/Boolean;I)V
      49: aload_1
      50: astore_3
      51: iload_2
      52: iconst_1
      53: iadd
      54: istore        4
      56: iconst_0
      57: istore        5
      59: iload         4
      61: istore        6
      63: aload_3
      64: arraylength
      65: istore        7
      67: iload         6
      69: iload         7
      71: if_icmpge     102
      74: aload_3
      75: iload         6
      77: aaload
      78: invokevirtual #93                 // Method java/lang/Boolean.booleanValue:()Z
      81: istore        8
      83: iconst_0
      84: istore        9
      86: iload         8
      88: ifeq          96
      91: iload         6
      93: goto          103
      96: iinc          6, 1
      99: goto          67
     102: iconst_m1
     103: istore_2
     104: goto          33
     107: aload_1
     108: astore_3
     109: iconst_0
     110: istore        4
     112: aload_3
     113: astore        5
     115: new           #95                 // class java/util/ArrayList
     118: dup
     119: invokespecial #98                 // Method java/util/ArrayList."<init>":()V
     122: checkcast     #100                // class java/util/Collection
     125: astore        6
     127: iconst_0
     128: istore        7
     130: aload         5
     132: astore        8
     134: iconst_0
     135: istore        9
     137: iconst_0
     138: istore        10
     140: iconst_0
     141: istore        11
     143: aload         8
     145: arraylength
     146: istore        12
     148: iload         11
     150: iload         12
     152: if_icmpge     234
     155: aload         8
     157: iload         11
     159: aaload
     160: astore        13
     162: iload         10
     164: iinc          10, 1
     167: aload         13
     169: astore        14
     171: istore        15
     173: iconst_0
     174: istore        16
     176: iload         15
     178: aload         14
     180: invokevirtual #93                 // Method java/lang/Boolean.booleanValue:()Z
     183: istore        17
     185: istore        18
     187: iconst_0
     188: istore        19
     190: iload         17
     192: ifeq          203
     195: iload         18
     197: invokestatic  #38                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     200: goto          204
     203: aconst_null
     204: dup
     205: ifnull        226
     208: astore        20
     210: iconst_0
     211: istore        21
     213: aload         6
     215: aload         20
     217: invokeinterface #104,  2          // InterfaceMethod java/util/Collection.add:(Ljava/lang/Object;)Z
     222: pop
     223: goto          227
     226: pop
     227: nop
     228: iinc          11, 1
     231: goto          148
     234: nop
     235: aload         6
     237: checkcast     #28                 // class java/util/List
     240: nop
     241: areturn

  private static final void crossOff(java.lang.Boolean[], int);
    Code:
       0: iload_1
       1: iload_1
       2: imul
       3: aload_0
       4: arraylength
       5: invokestatic  #140                // Method kotlin/ranges/RangesKt.until:(II)Lkotlin/ranges/IntRange;
       8: checkcast     #142                // class kotlin/ranges/IntProgression
      11: iload_1
      12: invokestatic  #146                // Method kotlin/ranges/RangesKt.step:(Lkotlin/ranges/IntProgression;I)Lkotlin/ranges/IntProgression;
      15: astore_2
      16: aload_2
      17: invokevirtual #149                // Method kotlin/ranges/IntProgression.getFirst:()I
      20: istore_3
      21: aload_2
      22: invokevirtual #152                // Method kotlin/ranges/IntProgression.getLast:()I
      25: istore        4
      27: aload_2
      28: invokevirtual #155                // Method kotlin/ranges/IntProgression.getStep:()I
      31: istore        5
      33: iload         5
      35: ifle          44
      38: iload_3
      39: iload         4
      41: if_icmple     55
      44: iload         5
      46: ifge          76
      49: iload         4
      51: iload_3
      52: if_icmpgt     76
      55: aload_0
      56: iload_3
      57: iconst_0
      58: invokestatic  #85                 // Method java/lang/Boolean.valueOf:(Z)Ljava/lang/Boolean;
      61: aastore
      62: iload_3
      63: iload         4
      65: if_icmpeq     76
      68: iload_3
      69: iload         5
      71: iadd
      72: istore_3
      73: goto          55
      76: return

  public static final <T> int indexOfFirst(T[], int, kotlin.jvm.functions.Function1<? super T, java.lang.Boolean>);
    Code:
       0: aload_0
       1: ldc           #161                // String <this>
       3: invokestatic  #167                // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
       6: aload_2
       7: ldc           #169                // String predicate
       9: invokestatic  #167                // Method kotlin/jvm/internal/Intrinsics.checkNotNullParameter:(Ljava/lang/Object;Ljava/lang/String;)V
      12: iconst_0
      13: istore_3
      14: iload_1
      15: istore        4
      17: aload_0
      18: arraylength
      19: istore        5
      21: iload         4
      23: iload         5
      25: if_icmpge     56
      28: aload_2
      29: aload_0
      30: iload         4
      32: aaload
      33: invokeinterface #175,  2          // InterfaceMethod kotlin/jvm/functions/Function1.invoke:(Ljava/lang/Object;)Ljava/lang/Object;
      38: checkcast     #82                 // class java/lang/Boolean
      41: invokevirtual #93                 // Method java/lang/Boolean.booleanValue:()Z
      44: ifeq          50
      47: iload         4
      49: ireturn
      50: iinc          4, 1
      53: goto          21
      56: iconst_m1
      57: ireturn

  public static void main(java.lang.String[]);
    Code:
       0: invokestatic  #181                // Method main:()V
       3: return
}


/**
 * Non-abundant sums
 *
 * A perfect number is a number for which the sum of its proper divisors is exactly equal to the number. For example,
 * the sum of the proper divisors of 28 would be 1 + 2 + 4 + 7 + 14 = 28, which means that 28 is a perfect number.
 *
 * A number n is called deficient if the sum of its proper divisors is less than n and it is called abundant if this sum
 * exceeds n.
 *
 * As 12 is the smallest abundant number, 1 + 2 + 3 + 4 + 6 = 16, the smallest number that can be written as the sum of
 * two abundant numbers is 24. By mathematical analysis, it can be shown that all integers greater than 28123 can be
 * written as the sum of two abundant numbers. However, this upper limit cannot be reduced any further by analysis even
 * though it is known that the greatest number that cannot be expressed as the sum of two abundant numbers is less than
 * this limit.
 *
 * Find the sum of all the positive integers which cannot be written as the sum of two abundant numbers.
 *
 * https://projecteuler.net/problem=23
 */
fun main() {
    println(nonAbundantSums())
}

private fun nonAbundantSums(): Int {
    val limit = 28123
    val totalSum = (1..limit).sum()
    val abundants = buildList {
        for (n in 12..limit) {
            if (n.sumOfProperDivisors() > n) {
                add(n)
            }
        }
    }

    val abundantsSums = buildSet {
        for (i in abundants.indices) {
            for (j in i..abundants.lastIndex) {
                val sum = abundants[i] + abundants[j]
                if (sum <= limit) {
                    add(abundants[i] + abundants[j])
                }
            }
        }
    }

    return totalSum - abundantsSums.sum()
}

Compiled from "Problem23.kt"
public final class Problem23Kt {
  public static final void main();
    Code:
       0: invokestatic  #10                 // Method nonAbundantSums:()I
       3: istore_0
       4: getstatic     #16                 // Field java/lang/System.out:Ljava/io/PrintStream;
       7: iload_0
       8: invokevirtual #22                 // Method java/io/PrintStream.println:(I)V
      11: return

  private static final int nonAbundantSums();
    Code:
       0: sipush        28123
       3: istore_0
       4: new           #24                 // class kotlin/ranges/IntRange
       7: dup
       8: iconst_1
       9: iload_0
      10: invokespecial #28                 // Method kotlin/ranges/IntRange."<init>":(II)V
      13: checkcast     #30                 // class java/lang/Iterable
      16: invokestatic  #36                 // Method kotlin/collections/CollectionsKt.sumOfInt:(Ljava/lang/Iterable;)I
      19: istore_1
      20: invokestatic  #40                 // Method kotlin/collections/CollectionsKt.createListBuilder:()Ljava/util/List;
      23: astore_3
      24: aload_3
      25: astore        4
      27: iconst_0
      28: istore        5
      30: bipush        12
      32: istore        6
      34: iload         6
      36: invokestatic  #46                 // Method Problem21Kt.sumOfProperDivisors:(I)I
      39: iload         6
      41: if_icmple     57
      44: aload         4
      46: iload         6
      48: invokestatic  #52                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      51: invokeinterface #58,  2           // InterfaceMethod java/util/List.add:(Ljava/lang/Object;)Z
      56: pop
      57: iload         6
      59: iload_0
      60: if_icmpeq     69
      63: iinc          6, 1
      66: goto          34
      69: nop
      70: aload_3
      71: invokestatic  #62                 // Method kotlin/collections/CollectionsKt.build:(Ljava/util/List;)Ljava/util/List;
      74: astore_2
      75: invokestatic  #68                 // Method kotlin/collections/SetsKt.createSetBuilder:()Ljava/util/Set;
      78: astore        4
      80: aload         4
      82: astore        5
      84: iconst_0
      85: istore        6
      87: iconst_0
      88: istore        7
      90: aload_2
      91: checkcast     #70                 // class java/util/Collection
      94: invokeinterface #73,  1           // InterfaceMethod java/util/Collection.size:()I
      99: istore        8
     101: iload         7
     103: iload         8
     105: if_icmpge     221
     108: iload         7
     110: istore        9
     112: aload_2
     113: invokestatic  #77                 // Method kotlin/collections/CollectionsKt.getLastIndex:(Ljava/util/List;)I
     116: istore        10
     118: iload         9
     120: iload         10
     122: if_icmpgt     215
     125: aload_2
     126: iload         7
     128: invokeinterface #81,  2           // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     133: checkcast     #83                 // class java/lang/Number
     136: invokevirtual #86                 // Method java/lang/Number.intValue:()I
     139: aload_2
     140: iload         9
     142: invokeinterface #81,  2           // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     147: checkcast     #83                 // class java/lang/Number
     150: invokevirtual #86                 // Method java/lang/Number.intValue:()I
     153: iadd
     154: istore        11
     156: iload         11
     158: iload_0
     159: if_icmpgt     202
     162: aload         5
     164: aload_2
     165: iload         7
     167: invokeinterface #81,  2           // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     172: checkcast     #83                 // class java/lang/Number
     175: invokevirtual #86                 // Method java/lang/Number.intValue:()I
     178: aload_2
     179: iload         9
     181: invokeinterface #81,  2           // InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
     186: checkcast     #83                 // class java/lang/Number
     189: invokevirtual #86                 // Method java/lang/Number.intValue:()I
     192: iadd
     193: invokestatic  #52                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     196: invokeinterface #89,  2           // InterfaceMethod java/util/Set.add:(Ljava/lang/Object;)Z
     201: pop
     202: iload         9
     204: iload         10
     206: if_icmpeq     215
     209: iinc          9, 1
     212: goto          125
     215: iinc          7, 1
     218: goto          101
     221: nop
     222: aload         4
     224: invokestatic  #92                 // Method kotlin/collections/SetsKt.build:(Ljava/util/Set;)Ljava/util/Set;
     227: astore_3
     228: iload_1
     229: aload_3
     230: checkcast     #30                 // class java/lang/Iterable
     233: invokestatic  #36                 // Method kotlin/collections/CollectionsKt.sumOfInt:(Ljava/lang/Iterable;)I
     236: isub
     237: ireturn

  public static void main(java.lang.String[]);
    Code:
       0: invokestatic  #110                // Method main:()V
       3: return
}


@file:Suppress("MagicNumber")

/**
 * Digit cancelling fractions
 *
 * The fraction 49/98 is a curious fraction, as an inexperienced mathematician in attempting to simplify it may
 * incorrectly believe that 49/98 = 4/8, which is correct, is obtained by cancelling the 9s.
 *
 * We shall consider fractions like, 30/50 = 3/5, to be trivial examples.
 *
 * There are exactly four non-trivial examples of this type of fraction, less than one in value, and containing two
 * digits in the numerator and denominator.
 *
 * If the product of these four fractions is given in its lowest common terms, find the value of the denominator.
 *
 * https://projecteuler.net/problem=33
 */
fun main() {
	println(isDigitCancellingFractions(numerator = 49, denominator = 98))
	println(findDigitCancellingFractionsProductDenominator())
}

private fun findDigitCancellingFractionsProductDenominator(): Int = buildList {
	// From 10 to 99, since we only support two digits.
	for (i in 11 until 100) {
		for (j in 10 until i) {
			if (isDigitCancellingFractions(numerator = j, denominator = i)) {
				add(j to i)
			}
		}
	}
}.reduce { (n1, d1), (n2, d2) ->
	(n1 * n2) to (d1 * d2)
}.let { (numerator, denominator) ->
	val denBI = denominator.toBigInteger()
	val gcd = numerator.toBigInteger().gcd(denominator.toBigInteger())
	(denBI / gcd).toInt()
}

private fun isDigitCancellingFractions(numerator: Int, denominator: Int): Boolean {
	check(numerator in 10..99)
	check(denominator in 10..99)
	check(numerator < denominator)

	val numDigits = numerator.digits().toSet()
	val denDigits = denominator.digits().toSet()
	val repeatedDigits = numDigits intersect denDigits
	if (numDigits.size == 1 || denDigits.size == 1 || repeatedDigits.size != 1) {
		return false
	}

	val digit = repeatedDigits.first()
	val newNumerator = numDigits.first { it != digit }.toDouble()
	val newDenominator = denDigits.first { it != digit }.toDouble()
	// ignore trivial fractions were common digit is zero
	return digit != 0 &&
		newDenominator != 0.0 &&
		(numerator.toDouble() / denominator.toDouble()) == (newNumerator / newDenominator)
}

Compiled from "Problem33.kt"
public final class Problem33Kt {
  public static final void main();
    Code:
       0: bipush        49
       2: bipush        98
       4: invokestatic  #10                 // Method isDigitCancellingFractions:(II)Z
       7: istore_0
       8: getstatic     #16                 // Field java/lang/System.out:Ljava/io/PrintStream;
      11: iload_0
      12: invokevirtual #22                 // Method java/io/PrintStream.println:(Z)V
      15: invokestatic  #26                 // Method findDigitCancellingFractionsProductDenominator:()I
      18: istore_0
      19: getstatic     #16                 // Field java/lang/System.out:Ljava/io/PrintStream;
      22: iload_0
      23: invokevirtual #29                 // Method java/io/PrintStream.println:(I)V
      26: return

  private static final int findDigitCancellingFractionsProductDenominator();
    Code:
       0: invokestatic  #35                 // Method kotlin/collections/CollectionsKt.createListBuilder:()Ljava/util/List;
       3: astore_0
       4: aload_0
       5: astore_1
       6: iconst_0
       7: istore_2
       8: bipush        11
      10: istore_3
      11: iload_3
      12: bipush        100
      14: if_icmpge     67
      17: bipush        10
      19: istore        4
      21: iload         4
      23: iload_3
      24: if_icmpge     61
      27: iload         4
      29: iload_3
      30: invokestatic  #10                 // Method isDigitCancellingFractions:(II)Z
      33: ifeq          55
      36: aload_1
      37: iload         4
      39: invokestatic  #41                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      42: iload_3
      43: invokestatic  #41                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
      46: invokestatic  #47                 // Method kotlin/TuplesKt.to:(Ljava/lang/Object;Ljava/lang/Object;)Lkotlin/Pair;
      49: invokeinterface #53,  2           // InterfaceMethod java/util/List.add:(Ljava/lang/Object;)Z
      54: pop
      55: iinc          4, 1
      58: goto          21
      61: iinc          3, 1
      64: goto          11
      67: nop
      68: aload_0
      69: invokestatic  #57                 // Method kotlin/collections/CollectionsKt.build:(Ljava/util/List;)Ljava/util/List;
      72: checkcast     #59                 // class java/lang/Iterable
      75: astore_0
      76: nop
      77: iconst_0
      78: istore_1
      79: aload_0
      80: invokeinterface #63,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
      85: astore_2
      86: aload_2
      87: invokeinterface #69,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
      92: ifne          105
      95: new           #71                 // class java/lang/UnsupportedOperationException
      98: dup
      99: ldc           #73                 // String Empty collection can\'t be reduced.
     101: invokespecial #77                 // Method java/lang/UnsupportedOperationException."<init>":(Ljava/lang/String;)V
     104: athrow
     105: aload_2
     106: invokeinterface #81,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     111: astore_3
     112: aload_2
     113: invokeinterface #69,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
     118: ifeq          216
     121: aload_3
     122: aload_2
     123: invokeinterface #81,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     128: checkcast     #83                 // class kotlin/Pair
     131: astore        4
     133: checkcast     #83                 // class kotlin/Pair
     136: astore        5
     138: iconst_0
     139: istore        6
     141: aload         5
     143: invokevirtual #86                 // Method kotlin/Pair.component1:()Ljava/lang/Object;
     146: checkcast     #88                 // class java/lang/Number
     149: invokevirtual #91                 // Method java/lang/Number.intValue:()I
     152: istore        7
     154: aload         5
     156: invokevirtual #94                 // Method kotlin/Pair.component2:()Ljava/lang/Object;
     159: checkcast     #88                 // class java/lang/Number
     162: invokevirtual #91                 // Method java/lang/Number.intValue:()I
     165: istore        8
     167: aload         4
     169: invokevirtual #86                 // Method kotlin/Pair.component1:()Ljava/lang/Object;
     172: checkcast     #88                 // class java/lang/Number
     175: invokevirtual #91                 // Method java/lang/Number.intValue:()I
     178: istore        9
     180: aload         4
     182: invokevirtual #94                 // Method kotlin/Pair.component2:()Ljava/lang/Object;
     185: checkcast     #88                 // class java/lang/Number
     188: invokevirtual #91                 // Method java/lang/Number.intValue:()I
     191: istore        10
     193: iload         7
     195: iload         9
     197: imul
     198: invokestatic  #41                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     201: iload         8
     203: iload         10
     205: imul
     206: invokestatic  #41                 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
     209: invokestatic  #47                 // Method kotlin/TuplesKt.to:(Ljava/lang/Object;Ljava/lang/Object;)Lkotlin/Pair;
     212: astore_3
     213: goto          112
     216: aload_3
     217: checkcast     #83                 // class kotlin/Pair
     220: astore_1
     221: iconst_0
     222: istore_2
     223: aload_1
     224: invokevirtual #86                 // Method kotlin/Pair.component1:()Ljava/lang/Object;
     227: checkcast     #88                 // class java/lang/Number
     230: invokevirtual #91                 // Method java/lang/Number.intValue:()I
     233: istore_3
     234: aload_1
     235: invokevirtual #94                 // Method kotlin/Pair.component2:()Ljava/lang/Object;
     238: checkcast     #88                 // class java/lang/Number
     241: invokevirtual #91                 // Method java/lang/Number.intValue:()I
     244: istore        4
     246: iload         4
     248: i2l
     249: invokestatic  #99                 // Method java/math/BigInteger.valueOf:(J)Ljava/math/BigInteger;
     252: dup
     253: ldc           #101                // String valueOf(...)
     255: invokestatic  #107                // Method kotlin/jvm/internal/Intrinsics.checkNotNullExpressionValue:(Ljava/lang/Object;Ljava/lang/String;)V
     258: astore        5
     260: iload_3
     261: i2l
     262: invokestatic  #99                 // Method java/math/BigInteger.valueOf:(J)Ljava/math/BigInteger;
     265: dup
     266: ldc           #101                // String valueOf(...)
     268: invokestatic  #107                // Method kotlin/jvm/internal/Intrinsics.checkNotNullExpressionValue:(Ljava/lang/Object;Ljava/lang/String;)V
     271: iload         4
     273: i2l
     274: invokestatic  #99                 // Method java/math/BigInteger.valueOf:(J)Ljava/math/BigInteger;
     277: dup
     278: ldc           #101                // String valueOf(...)
     280: invokestatic  #107                // Method kotlin/jvm/internal/Intrinsics.checkNotNullExpressionValue:(Ljava/lang/Object;Ljava/lang/String;)V
     283: invokevirtual #111                // Method java/math/BigInteger.gcd:(Ljava/math/BigInteger;)Ljava/math/BigInteger;
     286: astore        6
     288: aload         5
     290: aload         6
     292: invokestatic  #115                // Method kotlin/jvm/internal/Intrinsics.checkNotNull:(Ljava/lang/Object;)V
     295: aload         6
     297: invokevirtual #118                // Method java/math/BigInteger.divide:(Ljava/math/BigInteger;)Ljava/math/BigInteger;
     300: dup
     301: ldc           #120                // String divide(...)
     303: invokestatic  #107                // Method kotlin/jvm/internal/Intrinsics.checkNotNullExpressionValue:(Ljava/lang/Object;Ljava/lang/String;)V
     306: invokevirtual #121                // Method java/math/BigInteger.intValue:()I
     309: nop
     310: ireturn

  private static final boolean isDigitCancellingFractions(int, int);
    Code:
       0: bipush        10
       2: iload_0
       3: if_icmpgt     20
       6: iload_0
       7: bipush        100
       9: if_icmpge     16
      12: iconst_1
      13: goto          21
      16: iconst_0
      17: goto          21
      20: iconst_0
      21: ifne          34
      24: new           #146                // class java/lang/IllegalStateException
      27: dup
      28: ldc           #148                // String Check failed.
      30: invokespecial #149                // Method java/lang/IllegalStateException."<init>":(Ljava/lang/String;)V
      33: athrow
      34: bipush        10
      36: iload_1
      37: if_icmpgt     54
      40: iload_1
      41: bipush        100
      43: if_icmpge     50
      46: iconst_1
      47: goto          55
      50: iconst_0
      51: goto          55
      54: iconst_0
      55: ifne          68
      58: new           #146                // class java/lang/IllegalStateException
      61: dup
      62: ldc           #148                // String Check failed.
      64: invokespecial #149                // Method java/lang/IllegalStateException."<init>":(Ljava/lang/String;)V
      67: athrow
      68: iload_0
      69: iload_1
      70: if_icmpge     77
      73: iconst_1
      74: goto          78
      77: iconst_0
      78: ifne          91
      81: new           #146                // class java/lang/IllegalStateException
      84: dup
      85: ldc           #148                // String Check failed.
      87: invokespecial #149                // Method java/lang/IllegalStateException."<init>":(Ljava/lang/String;)V
      90: athrow
      91: iload_0
      92: invokestatic  #155                // Method Problem32Kt.digits:(I)Ljava/util/List;
      95: checkcast     #59                 // class java/lang/Iterable
      98: invokestatic  #159                // Method kotlin/collections/CollectionsKt.toSet:(Ljava/lang/Iterable;)Ljava/util/Set;
     101: astore_2
     102: iload_1
     103: invokestatic  #155                // Method Problem32Kt.digits:(I)Ljava/util/List;
     106: checkcast     #59                 // class java/lang/Iterable
     109: invokestatic  #159                // Method kotlin/collections/CollectionsKt.toSet:(Ljava/lang/Iterable;)Ljava/util/Set;
     112: astore_3
     113: aload_2
     114: checkcast     #59                 // class java/lang/Iterable
     117: aload_3
     118: checkcast     #59                 // class java/lang/Iterable
     121: invokestatic  #163                // Method kotlin/collections/CollectionsKt.intersect:(Ljava/lang/Iterable;Ljava/lang/Iterable;)Ljava/util/Set;
     124: astore        4
     126: aload_2
     127: invokeinterface #168,  1          // InterfaceMethod java/util/Set.size:()I
     132: iconst_1
     133: if_icmpeq     157
     136: aload_3
     137: invokeinterface #168,  1          // InterfaceMethod java/util/Set.size:()I
     142: iconst_1
     143: if_icmpeq     157
     146: aload         4
     148: invokeinterface #168,  1          // InterfaceMethod java/util/Set.size:()I
     153: iconst_1
     154: if_icmpeq     159
     157: iconst_0
     158: ireturn
     159: aload         4
     161: checkcast     #59                 // class java/lang/Iterable
     164: invokestatic  #172                // Method kotlin/collections/CollectionsKt.first:(Ljava/lang/Iterable;)Ljava/lang/Object;
     167: checkcast     #88                 // class java/lang/Number
     170: invokevirtual #91                 // Method java/lang/Number.intValue:()I
     173: istore        5
     175: aload_2
     176: checkcast     #59                 // class java/lang/Iterable
     179: astore        8
     181: iconst_0
     182: istore        9
     184: aload         8
     186: invokeinterface #63,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
     191: astore        10
     193: aload         10
     195: invokeinterface #69,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
     200: ifeq          245
     203: aload         10
     205: invokeinterface #81,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     210: astore        11
     212: aload         11
     214: checkcast     #88                 // class java/lang/Number
     217: invokevirtual #91                 // Method java/lang/Number.intValue:()I
     220: istore        12
     222: iconst_0
     223: istore        13
     225: iload         12
     227: iload         5
     229: if_icmpeq     236
     232: iconst_1
     233: goto          237
     236: iconst_0
     237: ifeq          193
     240: aload         11
     242: goto          255
     245: new           #174                // class java/util/NoSuchElementException
     248: dup
     249: ldc           #176                // String Collection contains no element matching the predicate.
     251: invokespecial #177                // Method java/util/NoSuchElementException."<init>":(Ljava/lang/String;)V
     254: athrow
     255: checkcast     #88                 // class java/lang/Number
     258: invokevirtual #91                 // Method java/lang/Number.intValue:()I
     261: i2d
     262: dstore        6
     264: aload_3
     265: checkcast     #59                 // class java/lang/Iterable
     268: astore        10
     270: iconst_0
     271: istore        11
     273: aload         10
     275: invokeinterface #63,  1           // InterfaceMethod java/lang/Iterable.iterator:()Ljava/util/Iterator;
     280: astore        12
     282: aload         12
     284: invokeinterface #69,  1           // InterfaceMethod java/util/Iterator.hasNext:()Z
     289: ifeq          334
     292: aload         12
     294: invokeinterface #81,  1           // InterfaceMethod java/util/Iterator.next:()Ljava/lang/Object;
     299: astore        13
     301: aload         13
     303: checkcast     #88                 // class java/lang/Number
     306: invokevirtual #91                 // Method java/lang/Number.intValue:()I
     309: istore        14
     311: iconst_0
     312: istore        15
     314: iload         14
     316: iload         5
     318: if_icmpeq     325
     321: iconst_1
     322: goto          326
     325: iconst_0
     326: ifeq          282
     329: aload         13
     331: goto          344
     334: new           #174                // class java/util/NoSuchElementException
     337: dup
     338: ldc           #176                // String Collection contains no element matching the predicate.
     340: invokespecial #177                // Method java/util/NoSuchElementException."<init>":(Ljava/lang/String;)V
     343: athrow
     344: checkcast     #88                 // class java/lang/Number
     347: invokevirtual #91                 // Method java/lang/Number.intValue:()I
     350: i2d
     351: dstore        8
     353: iload         5
     355: ifeq          399
     358: dload         8
     360: dconst_0
     361: dcmpg
     362: ifne          369
     365: iconst_1
     366: goto          370
     369: iconst_0
     370: ifne          399
     373: iload_0
     374: i2d
     375: iload_1
     376: i2d
     377: ddiv
     378: dload         6
     380: dload         8
     382: ddiv
     383: dcmpg
     384: ifne          391
     387: iconst_1
     388: goto          392
     391: iconst_0
     392: ifeq          399
     395: iconst_1
     396: goto          400
     399: iconst_0
     400: ireturn

  public static void main(java.lang.String[]);
    Code:
       0: invokestatic  #194                // Method main:()V
       3: return
}