How to put 2 loop inside a list? or a second loop to a second list

Asked

Viewed 153 times

-2

This is a program that checks between 1 to 100 which numbers are divisible by 3 and also counts their amount, all on top of this formula:

a=n*n;  
b=a+?;

This interrogation is where I wanted to loop to put any number from 1 to 100.

Code:

public static void main(String[] args) {

List<Long> Lista1 = new ArrayList();

for (long n = 1; n <= 100; n++) {

long a,b;

a = n*n; 
b = a+2;

if((b  % 3) == 0) {
    Lista1.add(b);
   } 

}
    System.out.println("Quantidade Números: "+Lista1.size());
    System.out.println("Divisível por 3: "+Lista1);

  }
}

Works normally: Funciona normalmente..

I can’t loop the "b".
b = a+2; is equal to 67 numbers...
Move:
b = a+3; is equal to 33 numbers. (quantity)
Changed, that is, it would need a Print, with a list of 1 to 100 of all quantities as the formula is changed (if you can change the sum of that formula with a loop).
the b being (a+1) = 0. No number from 1 to 100 is divisible by 3.

Now if you had made a list, from 1 to 3 of that "loop formula".

outworking:

1 = 0

2 = 67

3 = 33

that is to say...

b = a+1; = 0  
b = a+2; = 67  
b = a+3; = 33  

for a better understanding:

a = n*n; = number x number = 1*1 = 1
b = a+2; = 1+2 = 3

a = n*n; = number x number = 2*2 = 4
b = a+2; = 4+2 = 5

a = n*n; = number x number = 57*57 = 3249
b = a+2; = 3249+2 = 3251

Answer was Solved Thank you!

  • 1

    To tell the truth I so far did not understand the formula’s relation to find divisible by 3 between 1 and 100, what alias, can be made of much simpler way. Maybe if you edit and explain better how this formula works, it will help you understand the problem.

  • Doesn’t really make much sense. If you want to know which numbers between 1 and 100 are divisible by 3, then why have numbers greater than 100 in the results?

  • is simple, has bigger numbers because of the formula...

  • It is not simple, maybe it is for you, but for people who do not know very well what you are developing, it is difficult to understand, if it is not passed with more details.

  • blz I just edited the end of the question see there...

  • So you don’t want to check divisiles by 3 between 1 and 100, the context of the question doesn’t match the algorithm. If it is from 1 to 100, the largest divisible by 3 is 99, as can be seen by the link I posted.

  • yes it is that then it is difficult to understand even... and maybe it is not explaining correctly... but it would need to verify which numbers are divisible by 3, in the program without the other "loop" that I want is working normally... more I would need the "loop" which would be an additional...

  • Yes, without understanding the problem, it becomes complicated even to elaborate a solution as an answer. If you want to find divisible by 3 between 1 and 100, the algorithm is what I posted on the link. Your code is doing something else I didn’t even understand until now.

  • I’ll post another code so you understand what I want... ok... diegofm

  • It is not only the code that is the problem, missing you explain better what you are doing. In question says "checks between 1 to 100 which numbers are divisible by 3 and also counts their quantity", and the answer below does it perfectly. Explain better what the problem consists, the statement is not passing it in a way that can understand something other than the answer below.

Show 5 more comments

2 answers

0

If your goal is to print the numbers divisible by 3 from 1 to 100, this could be done much more simply:

public static void main(String[] args) {

List<Long> Lista1 = new ArrayList();

    for (long n = 3; n <= 100; n+=3) {
        Lista1.add(n);
    }

    System.out.println("Quantidade Números: "+Lista1.size());
    System.out.println("Divisível por 3: "+Lista1);

}
  • understand but this is not what I need =( is to find divisible by 3 between 1 to 100 within a "formula".

  • Finding numbers divisible by 3 between 1 and 100 is simple.. now inside a formula with "loop" that is difficult and I’m not getting understand... thanks for the answers!!

0


I don’t quite understand what you want to do. But see if this solves:

public static void main(String[] args) {

    List<Long> lista = null;

    for (long j = 1; j <= 5; j++) {
        lista  = new ArrayList<>();
        for (long n = 1; n <= 100; n++) {
            long a = n*n;
            long b = a+j;
            if((b % 3) == 0) {
                lista.add(b);
            }
        }
        System.out.println("J: "+j);
        System.out.println("Quantidade Números: "+lista.size());
        System.out.println("Divisível por 3: "+lista);
    }
 }
  • 2

    @Williamkarl you cannot edit an answer by asking another question. Understand, this question you are having is another question. You have to wait until you can ask a new question and then ask it. If you mix it all up it will confuse other readers. Although you have asked the question, other people may benefit from the answer. By the way, try to edit the question of this post so that it becomes clearer to future readers. Your question is marked as pending, try to resolve the pending.

  • 1

    @Williamkarl’s question limit is probably being applied because of the quality of the questions he posted today, his questions are being closed and negative. Try to do a little more. Try to understand the problem before posting, try to do a [mcve], define the text well, review before publishing. Follow the recommendations given by users, improve what you need. Without changing the meaning can improve the existing questions and try to reverse the votes. But be careful not to worsen the situation by changing the meaning of the question or by circumventing the functioning of the system.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.