-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);
}
}
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!
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.
– user28595
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?
– regmoraes
is simple, has bigger numbers because of the formula...
– William Karl
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.
– user28595
blz I just edited the end of the question see there...
– William Karl
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.
– user28595
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...
– William Karl
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.
– user28595
I’ll post another code so you understand what I want... ok... diegofm
– William Karl
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.
– user28595