30 lines
		
	
	
		
			806 B
		
	
	
	
		
			Plaintext
		
	
	
	
	
	
			
		
		
	
	
			30 lines
		
	
	
		
			806 B
		
	
	
	
		
			Plaintext
		
	
	
	
	
	
/*
 | 
						|
    Count "happy tickets": 6-digit numbers where sum of first three
 | 
						|
    digits is equal to those of last three. Just to demonstrate and
 | 
						|
    test the Lyng way. It is not meant to be effective.
 | 
						|
*/
 | 
						|
 | 
						|
fun naiveCountHappyNumbers() {
 | 
						|
    var count = 0
 | 
						|
    for( n1 in 0..9 )
 | 
						|
        for( n2 in 0..9 )
 | 
						|
            for( n3 in 0..9 )
 | 
						|
                for( n4 in 0..9 )
 | 
						|
                    for( n5 in 0..9 )
 | 
						|
                        for( n6 in 0..9 )
 | 
						|
                            if( n1 + n2 + n3 == n4 + n5 + n6 ) count++
 | 
						|
    count
 | 
						|
}
 | 
						|
 | 
						|
import lyng.time
 | 
						|
 | 
						|
//
 | 
						|
// After all optimizations it takes ~120ms.
 | 
						|
//
 | 
						|
for( r in 1..100 ) {
 | 
						|
    val start = Instant.now()
 | 
						|
    val found = naiveCountHappyNumbers()
 | 
						|
    println("Found happy numbers: %d time %s"(found, Instant.now() - start))
 | 
						|
    assert( found == 55252 )
 | 
						|
    delay(0.1)
 | 
						|
} |