- Insert comments into the UniAdd codes so that the meaning / purpose of the code is clearer.
File content not available for UniAdd.java
- How many ifs in total are used to add 111 with 111?
Since every step requires a if, counting the total number is just a matter of counting the distance each square travels. Breaking it down by task is probably the easiest:
- transfers: 8 = 4 + 4 (3+ the ending if)
- walkbacks: 12 = 1+2+3 + 1+2+3
- walkUps: 9 = 2+2+2 + 1+1+1
- addOnes: 21 = 1+2+3+4+5+6
- Total = 50
- Sometimes the random numbers are too big or too small to be interesting. Convert the code so that the numbers to be added are more than 2 and less than 8.
number {
4 {
choose 0.6 {
use n1
} { }
}
3 use n1
}
- Translate the code so that the sum displays in binary.
addOne {
sides 4
ifon blue {
paint yellow
use addOne
} {
paint blue
}
}
- Rewrite rNum so that it creates random binary numbers between 4 and 32.
number {
2 use getDigit
2 {
choose 0.6 {
use getDigit
} { }
}
use n1
}
getDigit {
choose 0.5 {
use n1
} {
use n0
}
}
- Rewrite AddNum and it's subprograms so that it correctly adds binary numbers.
transfer {
sides 4
ifon none { } {
2 create transfer
ifon blue {
right
create walkBack
} {
right
create zero
}
}
}
- Rewrite AddNum so it adds column-wise.
// uniAdd
start {
sides 4
paint red
right
sides 4
paint black
right
create blackLine
left
2 {
sides 4
paint black
right
create number
left
}
right
create addNum
}
blackLine {
sides 4
paint black
use blackLine
}
number {
4 {
choose 0.6 {
choose 0.2 {
use n1
} {
use n0
}
} { }
}
use n1
}
n1{
sides 4
paint blue
}
n0{
sides 4
paint yellow
}
addOne {
sides 4
ifon blue {
paint yellow
use addOne
} {
paint blue
}
}
addNum {
create transfer
right
sides 4
left
sides 4
left
create transfer
}
transfer {
sides 4
ifon none { } {
create transfer
ifon blue {
right
create walkBack
} {
right
create zero
}
}
}
walkBack {
sides 4
ifon black {
right
sides 4
left
sides 4
left
create addOne
} {
create walkBack
}
}
zero {
sides 4
ifon black {
sides 4
ifon none {
paint yellow
} { }
} {
create zero
}
}
- Compare the ifs used during the addition of 111 with 111 for each of the binary AddNums (non-columns and column-wise).
The column-wise count is 26 (I cheated I turned the uses into creates and counted the clicks). The non-columns count is at least 111.
- What are the biggest number that can be added (without changing the program with scaling etc) in the original UniAdd and the binary version of the code? What if ternary (base 3) was used instead?
- 10 squares means 10 is the biggest number in unary.
- 10 squares means 1111111111 = 2^11-1 = 2047 is the biggest number in binary.
- 10 squares means 2222222222 = 3^11-1 = 177138 is the biggest number in ternary.