#Solved

1 messages ยท Page 1 of 1 (latest)

warm yarrowBOT
#

<@&987246717831381062> please have a look, thanks.

warm yarrowBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

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.

regal pebble
#

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?

warm yarrowBOT
#

Detected code, here are some useful tools:

Formatted code
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);
  }
}
regal pebble
#

and what is n then?

#

in ur code, n = 50. what does that represent?

#

okay

atomic dirge
#

what you want in output?

regal pebble
#

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

atomic dirge
#

so this is giving wrong answer?

regal pebble
#

no worries

#

just follow the new design and it should be super easy

#

works on my side now

atomic dirge
regal pebble
#

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

atomic dirge
#

Actually ur logic is kinda wrong

regal pebble
#

๐Ÿ™‚

atomic dirge
atomic dirge
#

One thing, why you using while loop, who in the world uses that peepo_sad

regal pebble
#

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:

atomic dirge
regal pebble
#
  // inside loop:
  System.out.println("Row (" + height + "): " + rowStones);


// after loop:
System.out.println("Total stones: " + totalStones);
System.out.println("Height: " + height);
regal pebble
#

which OP doesnt have here

atomic dirge
#

yeah, but we can just omit some components, and for loop works then too

regal pebble
#

also, the loop should be rewritten to a while (true) loop

#

since the condition is checked inside, not outside

atomic dirge
#

like for(; true; ) lol

regal pebble
#

u should never write code like that

#

prefer a while loop in that situation instead

atomic dirge
#

ohk

#

i am just used to that

regal pebble
#

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

#

๐Ÿ‘

atomic dirge
regal pebble
#

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

atomic dirge
#

rowStones += (height+1); ??

regal pebble
#

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

atomic dirge
#

aaah

#

maybe I did something else

#

right

regal pebble
#

please dont post ur attempt

atomic dirge
#

he is not online tho

regal pebble
#

let OP figure it out themselves

atomic dirge
#

he is offline for a hour

regal pebble
#

then create a new thread or go to #geek-speak please

#

actually, lets move now

atomic dirge
#

yeah haha

warm yarrowBOT
#

Solved

#

Changed the title to Solved.

#

Closed the thread.