I am working on a lab for a class where a user inputs a number and it recursively prints out a number pattern. For example,
The base case is if they enter 1, it will print: 1
If they enter 2 it will print: 1 2 1
If 3, it will print: 1 2 1 3 1 2 1
and then for something bigger, if they enter 7, it will print:
1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 5 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 6
1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 5 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 7
1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 5 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 6
1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 5 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1
I'm a little stuck on what the number pattern is to be able to complete this problem. Does anyone have any ideas?
So you need to write a recursive function. Something of this form:
private String pattern(int num) {
// ...
}
The most important part is finding the right exit condition that should stop the recursion. In this case, that's when num == 1.
From the description, it looks like for a number k,
the output is pattern(k - 1) + k + pattern(k - 1).
I already spoiled too much.
You might need to improve the efficiency of this.
For example, realize that you don't need to run pattern(k - 1) twice,
it's enough to do it once.
I'm a little stuck on what the number pattern is to be able to
complete this problem.
Lets try to analyse the sequence using some function f
f(1) = 1 (Total digits = 1)
f(2) = 1 2 1 ( Total digits = 3)
f(3) = 121 3 121 (Total digits = 7)
f(4) = 1213121 4 1213121 (Total digits = 15)
f(5) = 121312141213121 5 121312141213121 (Total digits = 31)
So as you can observe total digits sequence looks like 1,3,7,15,31,....2^n-1
Now we can express this logic as mentioned below(Note : in order to help you to better understand how the program works i am printing sequence at every level)
public class SequenceGenerator {
public static void main(String[] args) {
generate(7);
}
static void generate(int depth) {
recursiveGenerator(1, null, depth);
}
static void recursiveGenerator(int num, String prev, int limit) {
if (num <= limit) {
if (prev != null) {
System.out.println();
}
if (prev != null) {
System.out.printf("%s %d %s", prev, num, prev);
} else {
prev = "";
System.out.printf("%d", num);
}
if (prev.equals("")) {
prev += num + prev;
} else {
prev += " " + num + " " + prev;
}
recursiveGenerator(++num, prev, limit);
}
}
}
Outputs
1
1 2 1
1 2 1 3 1 2 1
1 2 1 3 1 2 1 4 1 2 1 3 1 2 1
1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 5 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1
1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 5 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 6 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 5 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1
1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 5 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 6 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 5 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 7 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 5 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 6 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 5 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1
Related
I am calling this recursive function below:
public class formula {
public static void main(String[] args) {
int n = Integer.parseInt(args[0]);
sierpinski(n);
}
public static void sierpinski(int n) {
System.out.println(n);
if (n == 0) {
return;
}
else if (n > 0) {
sierpinski((n-1));
}
}
}
When I keep the argument as n-1 I understand that it would countdown normally but when I then change the code to
sierpinkski(3*(n-1));
I have the large values provided below.
C:\Users\joseph\Desktop\CS111 - 2020\cs111-a5>java formula.java 2
2
3
6
15
42
123
366
1095
3282
9843
29526
88575
265722
797163
2391486
7174455
21523362
64570083
193710246
581130735
1743392202
935209307
-1489339378
Is this because it is constantly multiplying by 3 each time it counts down and grows extremely large until there is a large enough value to get below zero and stop? If so how can I print it out so the values match this:
These are the instructions: Write a recursive function sierpinski() that takes one argument n, prints the value n, and then calls itself three times with the value n-1. The recursion should stop when n becomes 0.
This step is to help draw the full triangle but I want to understand the function before i continue. Thank you for your help and explanations.
sierpinksi(0)
sierpinksi(1)
1
sierpinski(2)
2
1
1
1
sierpinksi(3)
3
2 1 1 1
2 1 1 1
2 1 1 1
sierpinksi(4)
4
3 2 1 1 1 2 1 1 1 2 1 1 1
3 2 1 1 1 2 1 1 1 2 1 1 1
3 2 1 1 1 2 1 1 1 2 1 1 1
sierpinksi(5)
5
4 3 2 1 1 1 2 1 1 1 2 1 1 1 3 2 1 1 1 2 1 1 1 2 1 1 1 3 2 1 1 1 2 1 1 1 2 1 1 1
4 3 2 1 1 1 2 1 1 1 2 1 1 1 3 2 1 1 1 2 1 1 1 2 1 1 1 3 2 1 1 1 2 1 1 1 2 1 1 1
4 3 2 1 1 1 2 1 1 1 2 1 1 1 3 2 1 1 1 2 1 1 1 2 1 1 1 3 2 1 1 1 2 1 1 1 2 1 1 1
...and then calls itself three times with the value n-1... means you just need to duplicate calls to sierpinski((n-1)) one after the other 3 times as below:
sierpinski((n-1));
sierpinski((n-1));
sierpinski((n-1));
or you can call them in a loop
for ( int i = 0; i < 3; i++ ) sierpinski(n-1);
then you will see the pattern you want to see
My issue is that I have am creating a book recommendation system and when I try to square root the squares to determine similarity. I do not believe it is square rooting all the contents of each array.
The user is prompted with the twenty books and then inputs answers ranging from '1-5' based on how much they like the book and '-1' if they have not read the book.
A few of my score outputs are NaN. Therefore I assume it is just stopping after the first element of the array.
I have tried rearranging loops I personally think it is an issue with the loops and how it accesses the array.
Here is
CPU ratings file.
-1 1 1 4 1 3 3 1 2 3 4 -1 4 1 2 4 5 4 2 3
3 -1 2 3 -1 2 5 -1 3 3 5 2 2 1 2 3 5 3 4 2
-1 1 -1 4 1 3 5 2 1 5 3 -1 5 2 1 3 4 5 3 2
-1 -1 3 2 -1 5 5 2 2 4 4 2 3 2 -1 3 4 4 3 1
2 1 1 5 2 2 4 2 3 4 3 -1 5 2 2 5 3 5 2 1
3 -1 3 4 -1 2 5 -1 -1 4 3 -1 3 -1 2 5 5 5 4 2
4 -1 4 2 3 -1 1 3 4 -1 1 4 4 4 -1 2 -1 1 4 4
4 3 3 3 -1 2 2 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double []A) {
//check A for -1
double sum = 0;
for(int i = 0; i<A.length; i++) {
if(A[i] < 0 ) {
A[i] = 0;
}
A[i] = Math.sqrt(A[i]);
//calculate the running sum;
sum += A[i] * A[i] ;
}
return Math.sqrt(sum);
}
public static double similarity(double []A, double []B) {
double sum = 0;
double p1 = sqrtSquares(A);
double p2 = sqrtSquares(B);
for (int i=0; i<A.length; i++) {
if (A[i]> 0) {
if (B[i]> 0) {
sum += A[i]*B[i];
}
}
}
return sum/(p1*p2);
}
here is the main similarity score method
double []scores = new double[30];
for(int i = 0; i< 30; i++) {
scores[i] = similarity(yourrating, pplratings[i]);
}
for(int k = 0; k <scores.length; k++) {
System.out.println("SCORES ["+ k + "] "+scores[k]);
}
return scores;
}
In the end of the method it prints the 30 scores retrieved by both of the arrays. Here are the error results
SCORES [0] 0.8345932239467343
SCORES [1] 0.8930284538287845
SCORES [2] 0.8859571865530889
SCORES [3] 0.8885782312086968
SCORES [4] 0.8775173350115371
SCORES [5] 0.9443223415026459
SCORES [6] 0.8250453876017286
SCORES [7] 0.8432290780758503
SCORES [8] 0.8862288358972311
SCORES [9] 0.7131697319344704
SCORES [10] 0.8182594818515688
SCORES [11] 0.8009904274635006
SCORES [12] 0.8637068116707501
SCORES [13] 0.8507371827482269
SCORES [14] 0.8370334932826162
SCORES [15] 0.775738787468209
SCORES [16] 0.880315376993314
SCORES [17] 0.7702419338621114
SCORES [18] 0.841428935139835
SCORES [19] 0.7527243233023518
SCORES [20] 0.8474342113753683
SCORES [21] 0.815084547094269
SCORES [22] 0.7592956404693546
SCORES [23] 0.7303452808509205
SCORES [24] 0.7808981699861455
SCORES [25] 0.7676319325573738
SCORES [26] 0.7782147276497292
SCORES [27] 0.7962287074180334
SCORES [28] 0.7538710355467405
SCORES [29] 0.7795507063811014
EDIT: this code now works. Thank you for everyone's help.
public static double sqrtSquares(double []A) {
double sum = 0;
for(int i = 0; i<A.length; i++) {
if(A[i] < 0 ) {
A[i] = 0;
}
sum += A[i]*A[i]; // calculate the running sum of squares
}
return Math.sqrt(sum);
}
Based on cosine-similarity definition: https://en.wikipedia.org/wiki/Cosine_similarity
From our discussion, and your explanation of the problem, the following issues were found in your code.
The logic in the sqrtSquares() function was flawed. It still needs correction because you are implementing cosine similarity. The right definition is provided by #hsin1.att214. I am writing it here again, for convenience:
public static double sqrtSquares(double []A) {
double sum = 0;
for(int i = 0; i<A.length; i++) {
if(A[i] < 0 ) {
A[i] = 0;
}
sum += A[i]*A[i]; // calculate the running sum of squares
}
return Math.sqrt(sum); // calculate the square root of the sum of squares
}
The use of two return statements, one of which was inside the for loop, returns values after processing just the first element of the array. So pull the return statement outside the loop.
With a set of 8*8 2d array that is already assigned with random 1 and 2 ,after finding the first 1 in the first row,and change it to 0, how could a recursion of method use to change all the 1 surround to 0(from all direction) and stop when it touch 2.
for example
1 2 2 1 2 2 1 1
1 1 2 2 1 2 1 1
2 1 2 1 1 2 1 1
2 1 2 1 2 1 1 1
2 1 2 2 2 2 1 2
2 1 2 2 1 2 2 2
2 2 1 1 1 2 1 2
1 1 2 1 2 1 2 1
to
0 2 2 1 2 2 1 1
0 0 2 2 1 2 1 1
2 0 2 1 1 2 1 1
2 0 2 1 2 1 1 1
2 0 2 2 2 2 1 2
2 0 2 2 0 2 2 2
2 2 0 0 0 2 0 2
0 0 2 0 2 0 2 0
This is very similar to minesweeper.
You would start with which ever index you please and then there are 8 points you could possibly check.
Top left
Top
Top Right
Right
Bottom Right
Bottom
Bottom Left
Left
You would then call your recursive function, making sure that each new index that you are checking is a valid index of the array (Numbers in between and including 0 and length - 1).
For example:
public void reveal(int index1, int index2) {
// Quit if number is a 2
if(array[index1][index2] == 2)
return;
else {
// Check for correct index before calling
...
// Then
reveal(index1-1,index2-1);
reveal(index1-1,index2);
reveal(index1-1,index2+1);
reveal(index1,index2+1);
reveal(index1+1,index2+1);
reveal(index1+1,index2);
reveal(index1+1,index2-1);
reveal(index1,index2-1);
}
}
I've written a method that fills a bitmap represented by a m x n matrix. What I'm trying to do is to push the initial pixel to a stack, then in a while loop pop an element from the stack, color it and push neighboring pixels if they are the same color as the initial color of the initial pixel.
public void fill(int x, int y, char c) {
char tempColor = this.bitmap[y - 1][x - 1];
Point currentPoint;
Stack<Point> fillStack = new Stack<Point>();
fillStack.push(new Point(x, y));
do {
currentPoint = fillStack.pop();
// System.out.println(currentPoint.x + " " + currentPoint.y);
// System.out.println("Current state of the stack:");
// for (Point p: fillStack)
// System.out.println(p.x + " " + p.y);
this.bitmap[currentPoint.y - 1][currentPoint.x - 1] = c;
if (currentPoint.y - 1 > 0 && this.bitmap[currentPoint.y - 2][currentPoint.x - 1] == tempColor) {
fillStack.push(new Point(x, y - 1));
// System.out.println("Pushing " + currentPoint.x + " " + (currentPoint.y - 1));
}
if (currentPoint.y - 1 < n - 1 && this.bitmap[currentPoint.y][currentPoint.x - 1] == tempColor) {
fillStack.push(new Point(x, y + 1));
// System.out.println("Pushing " + currentPoint.x + " " + (currentPoint.y + 1));
}
if (currentPoint.x - 1 > 0 && this.bitmap[currentPoint.y - 1][currentPoint.x - 2] == tempColor) {
fillStack.push(new Point(x - 1, y));
// System.out.println("Pushing " + (currentPoint.x - 1) + " " + currentPoint.y);
}
if (currentPoint.x - 1 < m - 1 && this.bitmap[currentPoint.y - 1][currentPoint.x] == tempColor) {
fillStack.push(new Point(x + 1, y));
// System.out.println("Pushing " + (currentPoint.x + 1) + " " + currentPoint.y);
}
} while (!fillStack.isEmpty());
}
}
But it doesn't work for a reason I can't seem to spot. The output (debugging lines uncommented) is as follows:
3 3
Current state of the stack:
Pushing 3 2
Pushing 3 4
Pushing 4 3
4 3
Current state of the stack:
3 2
3 4
Pushing 4 2
Pushing 4 4
Pushing 5 3
4 3
Current state of the stack:
3 2
3 4
3 2
3 4
Pushing 4 2
Pushing 4 4
Pushing 5 3
4 3
Current state of the stack:
3 2
3 4
3 2
3 4
3 2
3 4
Pushing 4 2
Pushing 4 4
Pushing 5 3
4 3
Current state of the stack:
3 2
3 4
3 2
3 4
3 2
3 4
3 2
3 4
Pushing 4 2
Pushing 4 4
Pushing 5 3
4 3
Current state of the stack:
3 2
3 4
3 2
3 4
3 2
3 4
3 2
3 4
3 2
3 4
Pushing 4 2
Pushing 4 4
Pushing 5 3
4 3
Current state of the stack:
3 2
3 4
3 2
3 4
3 2
3 4
3 2
3 4
3 2
3 4
3 2
3 4
Pushing 4 2
Pushing 4 4
Pushing 5 3
4 3
Current state of the stack:
3 2
3 4
3 2
3 4
3 2
3 4
3 2
3 4
3 2
3 4
3 2
3 4
3 2
3 4
Pushing 4 2
Pushing 4 4
Pushing 5 3
4 3
Current state of the stack:
3 2
3 4
3 2
3 4
3 2
3 4
3 2
3 4
3 2
3 4
3 2
3 4
3 2
3 4
3 2
3 4
Pushing 4 2
Pushing 4 4
Pushing 5 3
4 3
Current state of the stack:
3 2
3 4
3 2
3 4
3 2
3 4
3 2
3 4
3 2
3 4
3 2
3 4
3 2
3 4
3 2
3 4
3 2
3 4
... and it goes on like this in an endless loop. What can be the problem?
Your print statements say one thing, your code does another! ;)
for example:
fillStack.push(new Point(x, y - 1));
System.out.println("Pushing " + currentPoint.x + " " + (currentPoint.y - 1));
See if you can spot the difference...
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
___________1
__________1 2 1
_________1 2 3 2 1
________1 2 3 4 3 2 1
______1 2 3 4 5 4 3 2 1
_____1 2 3 4 4 4 4 4 3 2 1
___1 2 3 3 3 3 3 3 3 3 3 2 1
__1 2 2 2 2 2 2 2 2 2 2 2 2 2 1
_1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
I would like to create this pyramid using java? Any suggestion?
This should do it:
public class Tower {
public static void main(String[] args) {
System.out.println(" 1 ");
System.out.println(" 1 2 1 ");
System.out.println(" 1 2 3 2 1 ");
System.out.println(" 1 2 3 4 3 2 1 ");
System.out.println(" 1 2 3 4 5 4 3 2 1 ");
System.out.println(" 1 2 3 4 4 4 4 4 3 2 1 ");
System.out.println(" 1 2 3 3 3 3 3 3 3 3 3 2 1 ");
System.out.println(" 1 2 2 2 2 2 2 2 2 2 2 2 2 2 1 ");
System.out.println(" 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ");
}
}
Try using a mono-spaced font like courier.
This will surve the purpose. You can change the number 5 to another number other than 5. eg. 1,2,3,.. , 6,8
public static void main(String[] args) {
List<String> list = new LinkedList<String>();
for(int i = 5; i > 0; i-- ){
wrapWithNumber(list, i);
}
for (String string : editListToBeInTriangleShape(list)) {
System.out.println(string);
};
}
/**
* Wrap the number strings in the llist with a perticular number.
* #param list list of Strings
* #param ba number which need to wrapp the list with.
*/
private void wrapWithNumber(List<String> list, final int ba) {
list.add(0, String.format("%d",ba));
for (int i = 1; i < list.size(); i++) {
String newformat = "%1$d " + list.get(i) + " %1$d";
list.remove(list.get(i));
list.add(i,String.format(newformat, ba));
}
String lastFormat = "%1$d";
for(int i = 0; i < 2 * list.size();i++){
lastFormat += " %1$d";
}
if(list.size() != 1) {
list.add(String.format(lastFormat, ba));
}
}
/**
* Arrage the Strings in the list in triangular manner.
* #param list list of Strings.
*/
private List<String> editListToBeInTriangleShape(final List<String> list) {
final List<String> returnList = new LinkedList<String>();
for (int i = list.size(); i > 0; i--) {
String s = list.get(list.size()-i);
int possition = list.size()*2 + s.length()/2;
returnList.add(String.format("%"+possition+"s", s));
}
return returnList;
}
out put of this :
1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1
1 2 3 4 4 4 4 4 3 2 1
1 2 3 3 3 3 3 3 3 3 3 2 1
1 2 2 2 2 2 2 2 2 2 2 2 2 2 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
I would suggest a series of for loops.