Count up in base 3 | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Count up in base 3

I am writing a java program, where I have an array, which I need to count up in base 3 sequence. eg. if the size of the array is 4, I would like to change it in following sequence- 0 0 0 0 0 0 0 1 0 0 0 2 0 0 1 0 0 0 1 1 0 0 1 2 0 0 2 0 0 0 2 1 0 0 2 2 0 1 0 0 0 1 0 1 . . 2 2 2 2 How to achieve this?

4th May 2018, 12:53 PM
Yash Gumaste
Yash Gumaste - avatar
2 Answers
0
Did this- static int[] IncIndexer(int[] indexer) { for(int i=indexer.length-1;i>=0;i--){ if(indexer[i]==2) { indexer[i]=0; } else { indexer[i]++; break;} } return indexer; }
5th May 2018, 1:59 AM
Yash Gumaste
Yash Gumaste - avatar
+ 1
You can also do the counting with a base 10 number, then translate each number to base 3. One way for changing the base is: https://code.sololearn.com/caG2AJ8WF88Q/?ref=app
4th May 2018, 11:17 PM
Pedro Demingos
Pedro Demingos - avatar