#Solved
1 messages ยท Page 1 of 1 (latest)
While you are waiting for getting help, here are some tips to improve your experience:
If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.
Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.
stone pyramid problem
Changed the title to stone pyramid problem.
please show ur full code, input and output
also, what are u counting? the total amount of stones?
or the stones of the last row?
or what?
Detected code, here are some useful tools:
public class YourClassNameHere {
public static void main(String[] args) {
int n = 50;
int count = 1;
int sum = 1;
for (int i = 2; i < n; i++) {
if (sum + i >= n) {
break ;
}
else {
sum = sum + sum + i;
count++;
}
}
System.out.print(count);
}
}
what you want in output?
ur code is hard to read, cause the variable names are quite confusing and there seems to be no clear concept to how u approached it
by just looking at the code
lets redesign it a bit
so first of all, u have ur n = 50. and u want to build the pyramid from scratch, counting the total stones. aborting once u go over n
so we start with sth like:
the key issue in ur code, readability-wise is that ur lacking a variable for the current rows stones
and perhaps one for the previous round
its much easier to compute if u track that
int n = 50;
int totalStones = 0;
int height = 0;
int rowStones = 0;
while (true) {
...
height++;
}
now its super simple, since u have the amount of stones of the previous row available
the stones for the current row are = old row + height
so
rowStones += height;
easy
now add it to totalStones
do the if-break check
and done
in ur current code, the logic is just completely flawed
u compute the stones as sum = sum + sum + i
which doesnt work at all
u have to do it based on the stones for the previous round instead
not the total stones
so this is giving wrong answer?
yes, it prints 5
no worries
just follow the new design and it should be super easy
works on my side now
because he used n = 50 in this code, what is output for n=50?
it should be 5
Row (1): 1
Row (2): 3
Row (3): 6
Row (4): 10
Row (5): 15
Total stones: 35 (then 56)
Height: 5
thats 25 though, not 50
Row (1): 1
Row (2): 3
Row (3): 6
Row (4): 10
Total stones: 20 (then 35)
Height: 4
as u see, works with the corrections
on my end
Actually ur logic is kinda wrong
we concluded that already and identified the bug
๐
alright ๐ฅน
One thing, why you using while loop, who in the world uses that 
thats incorrect. while loops are perfectly fine and used
almost there. add more debugging info and ull see whats wrong. u have to counter correct the number when u break
see here:
add these:
yeah, its just mostly peeps use for loops, they are well ... i can say.. explainatory
// inside loop:
System.out.println("Row (" + height + "): " + rowStones);
// after loop:
System.out.println("Total stones: " + totalStones);
System.out.println("Height: " + height);
only if u have all 3 components of the for loop
which OP doesnt have here
yeah, but we can just omit some components, and for loop works then too
also, the loop should be rewritten to a while (true) loop
since the condition is checked inside, not outside
like for(; true; ) lol
yes. but thats confusing AF
u should never write code like that
prefer a while loop in that situation instead
readers will not spot that u omitted one component and misinterpret, leading to bugs
anyways. back to OPs issue
add the above prints and ull quickly spot the issue
ur overall logic is correct now though
๐
just one mistake he did ig
yes, the core logic of sum = sum + sum + i is wrong and has to be made to tracking individual row-stones rowStones += height with totalStones += rowStones
rowStones += (height+1); ??
nah
the problem right now is that the height and stones were still incremented at the last row thats already beyond
so that has to be countered
with added debugging prints, its very easy to spot that
please dont post ur attempt
he is not online tho
let OP figure it out themselves
he is offline for a hour
yeah haha