Skip to main content
8 of 8
added 6 characters in body
138 Aspen
  • 7.3k
  • 1
  • 5
  • 18

Scala 3, 565 476 460 458 447 440 bytes

Golfed version. Attempt this online!

Saved 125 bytes (565->440) thanks to the comment of @ceilingcat

@main def m={val n=scala.io.StdIn.readInt();val M=Array.ofDim[Int](n,n);M(0)(0)=1;for(i<-1 until n;j<-1 until n){val z=M.slice(0,i+1);val x=z.map(_.slice(0,j+1));val y=z.map(_.slice(0,j+1));x(i)(j)=0;y(i)(j)=1;val d=1.0/(i+j+2);M(i)(j)=if(a(x,d)<a(y,d))0 else 1};M.foreach(o=>{o.foreach(l=>print(if(l<1)"."else 1));println("")});def a(m:Array[Array[Int]],d:Double)=math.abs(d*m.length*m.headOption.map(_.length).getOrElse(0)-m.flatten.sum)} 

Ungolfed version. Attempt this online!

import scala.io.StdIn.readInt object Main extends App { val n = readInt() val M = Array.ofDim[Int](n, n) M(0)(0) = 1 for (i <- 1 until n) { for (j <- 1 until n) { val x = M.slice(0, i + 1).map(_.slice(0, j + 1)) val y = M.slice(0, i + 1).map(_.slice(0, j + 1)) x(i)(j) = 0 y(i)(j) = 1 val d = 1.0 / (i + j + 2) M(i)(j) = if (math.abs(d - mean(x)) >= math.abs(d - mean(y))) 1 else 0 } } for (row <- M) { for (ele <- row) { if (ele == 0) print(".") else print(1) } println("") } def mean(matrix: Array[Array[Int]]): Double = { val rowCount = matrix.length val colCount = matrix.headOption.map(_.length).getOrElse(0) matrix.flatten.sum.toDouble / (rowCount * colCount) } } 
138 Aspen
  • 7.3k
  • 1
  • 5
  • 18