Lab 0(L): I Can, Therefore I Must JUnit: P2J12Test.java A jovial old grandpa mathematician straight out of central casting often appearing in Numberphile videos, Neil Sloane is behind the Online Encyclopedia of Integer Sequences, a wonderful tool for combinatorial explorations. This lab has you implement two methods to generate elements of two interesting integer sequences whose chaotic behaviour emerges from iteration of a deceptively simple rule. Both sequences are filled in ascending order in a greedy fashion, so that each element is always the smallest number that avoids creating a conflict with the previously generated elements in the sequence. Note that being defined by mathematicians, these sequences start from the position one instead of the position zero, the way how sequences work for us budding computer scientists who have to actually get our hands dirty and therefore prefer zero-based indexing. When asked to produce the first n elements of the sequence, these methods should create an array with n + 1 elements. The zeroth element that we don’t really care about is set to zero, after which all the fun begins. public static int[] forestFire(int n) Explained in the YouTube video “Amazing Graphs II”, the first two elements are both equal to one. Then, each element a[i] is the smallest positive integer for which no positive integer offset j creates an arithmetic progression of three equally spaced elements backwards in the sequence from the position i. More formally, the difference a[i]-a[i-j] may never equal the difference a[i-j]-a[i-2*j], even if your worst enemy got to pick the position i and the offset j. public static int[] remySigrist(int n) As explained in the YouTube video “Amazing Graphs III”, this sequence devised by Ré my Sigrist assigns to every positive integer a colour, encoded as a natural number. After the first colour assignment a[1]=0, each a[i] is the smallest natural number c for which the binary representation of any earlier position j that has already been assigned the colour c has any bits turned on common with the binary representation of the position i. This can be checked quickly with the expression i&j == 0, where & is the bitwise and operator of Java. This problem can be solved with two nested for-loops. The outer loop iterates through the positions of a. The inner loop counts upwards through the colours until it finds one that does not create a conflict. However, you should not be a “Shlemiel” who uses a third level inner loop to determine whether the current colour candidate c creates a conflict. Instead, you should trade space for time with a second integer array taken whose each element taken[c] gathers all the bits that are on in any previous position that was assigned the colour c. Then, whenever you to assign a[i]=c, update this lookup table with the assignment taken[c] |= i, where | is the bitwise or operator of Java, and |= is its assignment shorthand analogous to += and other such more familiar forms.
Lab 0(L): I Can, Therefore I Must
JUnit: P2J12Test.java
A jovial old grandpa mathematician straight out of central casting often appearing in Numberphile
videos, Neil Sloane is behind the Online Encyclopedia of Integer Sequences, a wonderful tool for
combinatorial explorations. This lab has you implement two methods to generate elements of two
interesting integer sequences whose chaotic behaviour emerges from iteration of a deceptively
simple rule. Both sequences are filled in ascending order in a greedy fashion, so that each element
is always the smallest number that avoids creating a conflict with the previously generated
elements in the sequence.
Note that being defined by mathematicians, these sequences start from the position one instead of
the position zero, the way how sequences work for us budding computer scientists who have to
actually get our hands dirty and therefore prefer zero-based indexing. When asked to produce the
first n elements of the sequence, these methods should create an array with n + 1 elements. The
zeroth element that we don’t really care about is set to zero, after which all the fun begins.
public static int[] forestFire(int n)
Explained in the YouTube video “Amazing Graphs II”, the first two elements are both equal to one.
Then, each element a[i] is the smallest positive integer for which no positive integer offset j
creates an arithmetic progression of three equally spaced elements backwards in the sequence
from the position i. More formally, the difference a[i]-a[i-j] may never equal the difference
a[i-j]-a[i-2*j], even if your worst enemy got to pick the position i and the offset j.
public static int[] remySigrist(int n)
As explained in the YouTube video “Amazing Graphs III”, this sequence devised by Ré my Sigrist
assigns to every positive integer a colour, encoded as a natural number. After the first colour
assignment a[1]=0, each a[i] is the smallest natural number c for which the binary
representation of any earlier position j that has already been assigned the colour c has any bits
turned on common with the binary representation of the position i. This can be checked quickly
with the expression i&j == 0, where & is the bitwise and operator of Java.
This problem can be solved with two nested for-loops. The outer loop iterates through the positions
of a. The inner loop counts upwards through the colours until it finds one that does not create a
conflict. However, you should not be a “Shlemiel” who uses a third level inner loop to determine
whether the current colour candidate c creates a conflict. Instead, you should trade space for time
with a second integer array taken whose each element taken[c] gathers all the bits that are on
in any previous position that was assigned the colour c. Then, whenever you to assign a[i]=c,
update this lookup table with the assignment taken[c] |= i, where | is the bitwise or operator
of Java, and |= is its assignment shorthand analogous to += and other such more familiar forms.
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 1 images