Groovy web console

subscribe to the feed Subscribe
to this
site
Calculate power of eleven (via #groovywebconsole)
tweet this snippet Tweet
this
script

Calculate power of eleven

Published 3 months ago by Luis G. Epifanio with tags multiply power of eleven
Actions  ➤ Edit in console Back to console Show/hide line numbers View recent scripts
String weirdMultiply(String input){
  if( ! (input =~ /\d+/) ){ 
    throw new IllegalArgumentException('It is not a number')
  }

  List<String> tokenized = input.toString()
      .split('')
      .findAll{ it }

  int index = tokenized.size() - 1
  int carry = 0
  def intermidiate = []
  
  while(index > 0){
    int last = tokenized[index].toInteger()
    int prelast = tokenized[index-1].toInteger()
    int sum = last + prelast + carry
    intermidiate << sum % 10
    //println( intermidiate.last() )
    carry = sum > 9 ? 1: 0
    index--
  }

  result = []
  result << ( tokenized[0].toInteger() + carry )
  result += intermidiate.reverse()
  result << tokenized[tokenized.size() -1] 

  // assert result.join().toInteger() == input * 11
  result.join()
}


String potencia (Integer exp){
    String partial = '1'    
    for( int i = 0; i < exp; i++ ){
        partial = weirdMultiply(partial)
    }
    partial
}

assert (11**1001).toString() == potencia(1001)

println("->"+11**1001)