How to resolve the algorithm Happy numbers step by step in the Dart programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Happy numbers step by step in the Dart programming language

Table of Contents

Problem Statement

From Wikipedia, the free encyclopedia:

Find and print the first   8   happy numbers. Display an example of your output here on this page.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Happy numbers step by step in the Dart programming language

Source code in the dart programming language

main() {
  HashMap<int,bool> happy=new HashMap<int,bool>();
  happy[1]=true;

  int count=0;
  int i=0;

  while(count<8) {
    if(happy[i]==null) {
      int j=i;
      Set<int> sequence=new Set<int>();
      while(happy[j]==null && !sequence.contains(j)) {
        sequence.add(j);
        int sum=0;
        int val=j;
        while(val>0) {
          int digit=val%10;
          sum+=digit*digit;
          val=(val/10).toInt();
        }
        j=sum;
      }
      bool sequenceHappy=happy[j];
      Iterator<int> it=sequence.iterator();
      while(it.hasNext()) {
        happy[it.next()]=sequenceHappy;
      }
    }
    if(happy[i]) {
      print(i);
      count++;
    }
    i++;
  }
}


  

You may also check:How to resolve the algorithm Parsing/Shunting-yard algorithm step by step in the XPL0 programming language
You may also check:How to resolve the algorithm Window creation step by step in the Emacs Lisp programming language
You may also check:How to resolve the algorithm MD4 step by step in the Perl programming language
You may also check:How to resolve the algorithm Command-line arguments step by step in the CoffeeScript programming language
You may also check:How to resolve the algorithm Remove lines from a file step by step in the Phixmonti programming language