Solve Number Puzzle with C and Java - java

This question is translated into English by me from another forum, I found it interesting and then just write a Java solution. And found there's some heap size problem when dealing with large number like 10000000. And I would like to seek some really smart solution compared with my own.
Original Post is in Chinese. And I kind of revised it a little based on my understanding to make it clearer.
http://zhidao.baidu.com/question/1637660984282265740.html?sort=6&old=1#here
Below is the puzzle:
10000 rows of numbers;
1 row: 2,4,6,8...2K(2K<=10000000); (numbers no repeats for this row)
2 row: 3,3,6,6,9,9...3K(3K<=10000000); (starting from this row, each number repeats 2 times and a multiple which has something to do with row number (2XrowNumber-1) to be specificaly)
3 row: 5,5,10,10,15,15...5K(5K<=10000000);
and following 7K,9K,11K,13K....until
10000 row: 19999,19999,39998,39998....19999K,19999K (19999K<=10000000);
That's all the rows to be used in the following part. And now we will calculate the repeat times of numbers starting from row 1 and row 2:
Integer w1 is the repeat times of numbers in row 1 and row2. For example, consider row 1 numbers 2,4,6 and row 2 numbers 3,3,6,6. Then the repeat times up to this point would be 3 since 6 is already in row 1 and appears 2 times in row 2, and 3 appears 2 times in row 2;
Integer w2 is the repeat times of numbers in row 1 and row 2 and row 3.
Integer w3 is the repeat times of numbers in row 1 and row 2 and row 3 and row 4.
......
Integer w9999 is the repeat times of numbers of row 1,row 2,row 3 .....row 10000.
And now print out all integers, w1,w2....w9999;
I have come up with one Java solution, but I have heap size problem since 10000000 is too large and the memory is not enough. So I just use 10000 instead of 10000000, and 10 instead of 10000. Below is what I write in Java. I guess it should be right (if not, please point it out):
Set nums = new HashSet();
int max = 10000;
int row = 10;
for (int i=2;i<=max;i+=2){
nums.add(new Integer(i));
}
int nums_size = nums.size();
int w = 0;
for (int i=2;i<=(row);i++){
int tmp_count = 0;
int self_count = 0;
for (int j=(2*i-1);j<=max;j+=(2*i-1)){
nums.add(new Integer(j));
self_count++;
if (nums.size()==nums_size){
tmp_count++;
} else {
nums_size = nums.size();
}
}
w += tmp_count;
w += self_count;
System.out.println("w"+(i-1)+": "+w);
}
My question is
How to get a better solution in Java (if any)?
How to do it in C
since there would be no Set class in C as I remember. (importing 3rd
party library would not be preferred)?
Thanks.

Here is a simplified version of your code. Since it doesn’t use HashSet, creating a C version out of it should be no problem anymore.
int max = 10000;
int row = 10;
boolean[] seen=new boolean[max+1];
for(int i=2;i<=max;i+=2) seen[i]=true;
int w = 0;
for(int i=2;i<=(row);i++) {
int self_count = 0;
for(int j=(2*i-1);j<=max;j+=(2*i-1)) {
self_count++;
if(seen[j]) w++; else seen[j]=true;
}
w += self_count/2;
System.out.println("w"+(i-1)+": "+w);
}

Not an answer, but a hint: try using least common multiples.
For example, LCM(5,3)=15, and 15 is the first element in both rows 2 and 3. LCM(2,15)=30, and 30 is the first element in each of the first 3 rows.
You'll eventually get to a row r where the LCM of the first element of the first r rows is beyond 10,000,000, at which point no number shows up each of those rows.

Related

Adding each column in a 2D array which become values of the last row

I'm trying to add all of the values for each column in a 2D array and these sums become values that overwrite the last row of the array
for example:
4 5 6 7 8
1 2 3 4 5
0 0 0 0 0 //this row will be replaced by the sum of each column
4 5 6 7 8
1 2 3 4 5
5 7 9 11 13
public static void fillTotals(int[][] scores)
{
int count = 0;
for (int r = 0; r < scores.length - 1; r++)
{
scores[r][0] += count;
scores[scores.length - 1][scores[0].length - 1] = count;
}
}
I thought I could keep the columns the same and add it down with the changing rows but it isn't rewriting the last row. Also I don't know how to change the values at the bottom
You need to iterate once over all rows and columns, actually iterate over all rows, for every column. If you assume that the number of columns is the same for every row, then you can use scores[0].length as a fixed value.
public static void fillTotals(int[][] scores) {
for (int c=0; c < scores[0].length; ++c) {
int sum = 0;
for (int r=0; r < scores.length - 1; ++r) {
sum += scores[r][c];
}
scores[scores.length - 1][c] = sum;
}
}
This assumes that the final row of the 2D array is not part of the sum and is available to be overwritten with the sum of all preceding values, for each column.
Well, the reason that nothing is being updated is that you never change count, so you just end up adding 0 to everything. I think what you want instead of:
scores[r][0] += count;
is:
count += scores[r][0];
That way count will contain the summation of every element in the first column.
To be clear, scores[r][0] += count; is the same as scores[r][0] = scores[r][0] + count;, whereas I think you probably want count = scores[r][0] + count;
That being said, Im still pretty sure this code isnt actually going to work (sorry), since you only ever actually sum values from the first column. However, for the sake of not just doing way may be a school assignment for you, Im just going to leave it there. If you're still stuck let me know, and I'll try to help!

How to initialize a large number of variables in Java quickly and easily?

So I have a really basic coding question, I just started learning this year, and I have an assignment for a code that is supposed to
flip a fair coin n times and count how many heads it gets. The program will do m experiments and then
print out the results as a (vertical) histogram. The program will first ask the user to input the number of
experiments to perform (m) and the number of coin flips in each experiment (n). n can be at most 79. The
output will be a histogram made up of spaces and asterisks (*), with n+1 columns (for the number of heads
which can go from 0 to n)
I have the code for the histogram all done, but the problem I have is how to store the number of heads as it's own variable. For example, if it flipped the coin 80 times and 40 of them were heads, I would want it to create an int for 40 flips, then add one to it. I just don't know how to go about initializing 80 variables without writing out int one = 0; int two = 0, int three = 0; until the end of time, then writing 80 if statements until it finds the right integer to add one to. Is there an easy way to do this or should there be a different approach I should be taking? Here is the code, please be gentle, literally only a month or so into an extremely basic java class
for(m=m; m>0; m--) { //runs m number of experiments of n coinflips, keeping track of total heads
n = o; // when N gets set to 0 through the while loop, o resets it back to it's original so it can loop again
while(n>0) {
n--;
if(random.nextBoolean()) {
heads++;
total++;
/** here is where I want the number of heads saved to a
variable, so that way later I can run a loop to put as many *'s as I need in the histogram later */
Just use an array for the 80 (or n) experiments:
I don't understand, this would just count how many heads/tails there are right? This doesn't save them (ie it flipped 5 heads 6 times, 4 heads 3 times, 3 heads twice, ect) unless I'm misunderstanding
If you are storing the number of head m times (where m is < 80), you can:
1) print the histogram as you generate the results (no array needed) OR
2) store the 80 results in an array
Example for 1 (no array):
for(int x=0; x<experiments; x++){
int heads = 0;
for(int y=0; y<flips; y++){
if(rnd.nextInt(2) == 0) //assume 0 is head
heads ++;
}
//print histogram
for(int y=0; y<heads; y++)
System.out.print("*");
System.out.println();
}
Example for 2 (with array):
int[] expr = new int[80]; //store results for 80 experiments
for(int x=0; x<expriments; x++)
for(int y=0; y<flips; y++)
if(rnd.nextInt(2) == 0)
expr[x] ++;
Use an array:
int n = 80;
// space for n integers, with indexes 0..n
int[] histogram = new int[n + 1];
for (int i = 0; i < experiments; i++) {
// flip coin n times, counting heads
int heads = ...;
histogram[heads] = histogram[heads] + 1;
}
for (int i = 0; i < histogram.length; i++) {
printStars(histogram[i]);
}
If you're unfamiliar with arrays, the Java Tutorial has a good explanation.

Adding two integers together using only 1's

http://puu.sh/8ekfm.png
Zonko's government (The Kingdom of Zumbania) decided to ban directly adding numbers greater than 1 to other numbers. Zonko decided to create his own addition method (for positive integers) to circumvent the ban. Can you help finish his code?
To get the sum of a and b, Zonko starts by setting a variable sum equal to a. He then repeatedly adds 1 to sum until it reaches a+b. 1 is added every iteration of the loop until its been added the right number of times. What is the correct condition in the loop so it stops at the right time?
public int add(int a, int b){
int sum = a;
for(int i=1; LOOP-CONDITION; i=i+1){
sum = sum + 1; //this will add 1 to sum every iteration
}
return sum;
}
Can someone explain this and tell me how they got the answer?
The Java idiomatic for loops to perform an action a certain number of times are:
for(int i=1; i <= [numTimes]; i=i+1){
and
for(int i=0; i < [numTimes]; i=i+1){
The former has the correct starting condition, so your loop condition would be: i <= b.
The trick is in the words :
Zonko's government (The Kingdom of Zumbania) decided to ban directly
adding numbers greater than 1 to other numbers.
This means a cannot be added to b directly when b is greater than 1.
Assume you want to add a = 5 and b = 3. You can get the answer by adding 1 to 5 three times.
a = 5;
loop(b times){
a = a + 1;
}
Here is how the loop will go:
a = 5;
loop starts
a = 5 + 1;
a = 6 + 1; // we made it 6 in previous step
a = 7 + 1;
loop ends

Java algorithm query

I'm wanting to find the average difference of the array totals.
My array structures are as follows (pulled from a database):
day, 1, 2, 3, 4
So you can see the 5 arrays how it's pulled from the database.
Now for instance, the Java algorithm I need is as follows:
To add each of the 1,2,3,4 values up, then loop to the next day (if there is one), repeating the same addition. Then eventually, calculate the 'average difference' between the totals of each day.
So for example, i've done some sample data to try and make it clearer for you!
1 (Mon) 135 90 105 150
2 (Tues) 143 86 117 163
3 (Wed) 129 100 140 158
So what the algorithm needs to do is:
135+90+105+150 = 500,
143+86+117+163 = 509,
129+100+140+158 = 527
But the only problem, is that, column 1 is its own array and column 2,3,4 preceding.
Calculate the 'average increase/decrease' which would be:
https://math.stackexchange.com/questions/16554/what-is-average-increase-percentage-and-how-to-calculate-it
I'm unsure how to implement it into Java! Any help would be appreciated.
If i can provide any more information please let me know.
Many thanks.
Have you arrived at this stage first of all to be able to access all the elements? (if I understood your question correctly?)
//first for loop loops through the number of days
for (int i = 0; i < day.length; i++)
{ //second for loop loops through all integers in Ith day
for (int j = 0; j < day[i].length; j++)
{
print day[i][j]
}
}
Since we know the location of the items in the database, the following might work:
/*Psudeocode:
ArrayList<Integer> firstCol = //get first column
ArrayList<Integer> secondCol = //get second column
....
ArrayList<Integer> nCol = //get n column
*/
ArrayList<Integer> values = new ArrayList<Integer>();
int currentRow;
for(int i=0;i<n;i++)
{
currentRow = 0;
currentRow += firstCol.get(i);
currentRow += secondCol.get(i);
//etc, etc...
values.add(currentRow);
}
//At this point, values contains all the sums of the different rows.
//To calculate the average percent change:
//PR= (((Vpresent - Vpast) / Vpast) x 100) / N
for(int i=0;i<values.size() -1;i++)
{
System.out.println(((values.get(i+1) - values.get(i)) * 100) / (i+1))
}
I hope this helps. Help with Average Percent Change

Matrix Multiplication Java

I need help, I am trying to make use of Lattice Multiplication in java for use in a BigInt class I am having to write for a class.
Right now I have the code storing the digits needed for the adding part of the algorithm in a 2 dimensional array. From there though I am at a lose as to how to make a loop to go through the array and add the numbers in what would be a diagonal.
For instance here is the test numbers etc:
200
*311
= 62200
The array is holding:
6 0 0
2 0 0
2 0 0
6 is (2,2) in the array and the bottom right is (0,0)
I need to add in a diagonal, such as (1,0) + (0,1) = 0
The issue is how do I do this, since not only is it moving up and left in different ways, but it goes from 1 element to 2 elements to 3 elements, then back the other way, and of course this will get bigger the longer the number is.
This is the code that I have:
public int multiply(BigInt val){
int[] bigger;
int[] smaller;
int[] dStore;
int lengthMax = (val.getSize()+this.getSize()) - 1;
int first = 0;
int second = 0;
int[][] tempResult;
//Checks to see which is bigger and then adds that to bigger
if(val.getSize() >= this.getSize()){
bigger = val.getData();
smaller = this.getData();
dStore = new int[val.getSize()+this.getSize()];
}else{
bigger = this.getData();
smaller = val.getData();
dStore = new int[val.getSize()+this.getSize()];
}
tempResult = new int[smaller.length][bigger.length];
for(int i=0;i < smaller.length;i++){
for(int j = 0;j < bigger.length;j++){
tempResult[i][j] = smaller[i] * bigger[j];
}
}
** there is the return statement etc below
This might help as to explain lattice multi better: Lattice Multi Video
I would try a different approach. Look at the lattice in the video and imagine that you rotates the array a little bit to the left so that the diagonals becomes vertical. The array would then look like this:
2 3 5
8 3
2 4 0
Now, just summarize the columns and you would have the total.
You would of course have to split the numbers into arrays of digits first. The easiest way to do that (but not the most efficient) is to convert them into strings ...
Good luck!
To move diagonally, you'd increment both x and y:
// Loop though the diagonal of an array
x = 0;
y = 0;
while (x < ARR_X_SIZE && y < ARR_Y_SIZE) {
// Do something with arr[x][y]
x++;
y++;
}
This is the basic loop; you can change the x and y increments to determine the direction you need to go. The key to going through the whole array is the value of the coordinates going into the loop. Array:
1 2 3
4 5 6
7 8 9
If you set x = 1; y=0 at the beginning of the loop, you'll get 2 6. Set x = 0, y = 1 and you'll get 4 8.
I hope this helps you with your assignment. Good luck on the rest! That is definately an interesting algorithm to implement.

Categories

Resources