How to print a list with the result of all numbers in a sequence summed with itself? (sequence)

Asked

Viewed 210 times

-7

I have this sequence of numbers: 1,2,3. (Can be from 1 to infinity.)

I want to add every number in the sequence to it all, example:

The sequence has 3 numbers.

You have to add in this way:

(1)  
1+1=2  
1+2=3  
1+3=4  

(2)  
2+1=3  
2+2=4  
2+3=5 

(3)  
3+1=4  
3+2=5  
3+3=6 

The program shall print the list with the results of each sequence number separately:

1=(2,3,4)
2=(3,4,5)
3=(4,5,6)

  • 1

    To Victor response already seems to do it. She did not answer?

  • Good Afternoon @diegofm, the output of the program did not "print" the "list" with "all" the "results" of the "sums" of each "number" of the "sequence".

  • @diegofm Victor’s program does a check of which result appears most often in the sums of the numbers of a sequence with itself but I have not printed a list with the results of the sums of each sequence number with itself...

  • 2

    @Felipecortez the idea of Stackoverflow is to help solve bugs and find workarounds for problems, and not do your school task!

  • 1

    Sorry @I'mBlueDaBaDee but I’m not doing school homework. I’m trying to build a program but I can’t and I’m asking for help, that’s all... Don’t get angry or angry with me if you can’t help me, I can help you, I’ll help you by giving you advice: Look, help your neighbor while you still can, while you still have time, because one hour all of our time here is over, and at the end of the day we all regretted what we didn’t do and could have done... And when will we be sure of all this? Just on the other side of life... Until more friend...

  • Which language are you trying to implement? javascript, java, python, c++, ruby or all? What have you tried? Which error has occurred?

  • @Felipecortez at no time did I leave my cordiality when addressing it, was a comment on how trivial is your question, what you do seem with which to be a schoolwork, if you wish nay pass this impression, show the context of what you are doing, what your goal is with such functionality. At.te. Lucas Gabriel da Costa.

  • 1
  • Boy, had the gift of downvote without having the question closed, new Achievment

  • Good evening @diegofm I will leave you the link with the program update: [https://answall.com/questions/212317/howto verify any results_appear%C3%A1-more-times-in-a-time-sum%C3%B3gica-de-um]

  • Good evening @I'mBlueDaBaDee, follow the link with the program update: [https://answall.com/questions/212317/howto verify any results-appear%C3%A1-more-times-in-a-time-sum%C3%B3gica-de-um]

  • Good evening @Fabiano, I am implementing the program in Java, it is almost finished I will leave the link of the update, thanks for the help... [https://answall.com/questions/212317/howto verify any results-appear%C3%A1-more-times-in-a-time-sum%C3%B3gica-de-um]

Show 7 more comments

1 answer

1


I believe this example does what you need. The class receives the list of elements in the constructor. The sum method iterates twice in a nested form by the list and prints the result.

import java.util.ArrayList;
import java.util.List;

public class SomaLista {

    private List<Integer> lista = null;

    public SomaLista(List<Integer> lista) {
        this.lista = lista;
    }

    public void soma() {
        for (Integer externo : lista) {
            System.out.print(externo + "=");
            List<String> resultado = new ArrayList<String>();
            for (Integer interno : lista) {
                resultado.add(String.valueOf(interno + externo));
            }
            System.out.println("("+String.join(",", resultado)+")");
        }
    }

    public static void main(String args[]) {
        List<Integer> lista = new ArrayList<Integer>();

      for (int i=1; i<=1000000; i++){
  lista.add(i);
        }
        SomaLista sl = new SomaLista(lista);
        sl.soma();
    }

}
  • Good Afternoon @fsola, thank you very much is almost that!! is almost 90%.. The only problem is that the list as there is is from 1 to 3.. and if it’s from 1 to 1,000,000 it would be difficult to put one by one.. it would have to be something like an array.. How to do all this you did, with an array that had the setting you wanted?

  • Put the.add list inside a for up to 1000000.

  • Thank you very much!!! I managed to put the list in a for... that was it!!!

  • Good evening @fsola, I updated my first question to include an implementation in the program: [https://answall.com/questions/212317/howto verify any results]

Browser other questions tagged

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