Iterate through each digit in a number

后端 未结 5 741
星月不相逢
星月不相逢 2020-12-15 07:41

I am trying to create a program that will tell if a number given to it is a \"Happy Number\" or not. Finding a happy number requires each digit in the number to be squared,

相关标签:
5条回答
  • 2020-12-15 07:46

    This code returns the first number (after 1) that fits your description.

    public static void main(String[] args) {
        int i=2;
        // starting the search at 2, since 1 is also a happy number
        while(true) {
            int sum=0;
            for(char ch:(i+"").toCharArray()) { // casting to string and looping through the characters.
                int j=Character.getNumericValue(ch);
                // getting the numeric value of the current char.
                sum+=Math.pow(j, j);
                // adding the current digit raised to the power of itself to the sum.
            }
            if(sum==i) {
                // if the sum is equal to the initial number
                // we have found a number that fits and exit.
                System.out.println("found: "+i);
                break;
            }
            // otherwise we keep on searching
            i++;
        }
    }
    
    0 讨论(0)
  • 2020-12-15 07:51

    You can use a modulo 10 operation to get the rightmost number and then divide the number by 10 to get the next number.

    long addSquaresOfDigits(int number) {
        long result = 0;
        int tmp = 0;
        while(number > 0) {
            tmp = number % 10;
            result += tmp * tmp;
            number /= 10;
        }
        return result;
    }
    

    You could also put it in a string and turn that into a char array and iterate through it doing something like Math.pow(charArray[i] - '0', 2.0);

    0 讨论(0)
  • 2020-12-15 07:57

    Assuming the number is an integer to begin with:

    int num = 56;
    String strNum = "" + num;
    int strLength = strNum.length();
    int sum = 0;
    
    for (int i = 0; i < strLength; ++i) {
      int digit = Integer.parseInt(strNum.charAt(i));
      sum += (digit * digit);
    }
    
    0 讨论(0)
  • 2020-12-15 07:59

    In the above example we can use:

    int digit = Character.getNumericValue(strNum.charAt(i));
    

    instead of

    int digit = Integer.parseInt(strNum.charAt(i));
    
    0 讨论(0)
  • 2020-12-15 08:02

    I wondered which method would be quickest to split up a positive number into its digits in Java, String vs modulo

      public static ArrayList<Integer> splitViaString(long number) {
    
        ArrayList<Integer> result = new ArrayList<>();
        String s = Long.toString(number);
    
        for (int i = 0; i < s.length(); i++) {
          result.add(s.charAt(i) - '0');
        }
        return result; // MSD at start of list
      }
    

    vs

      public static ArrayList<Integer> splitViaModulo(long number) {
    
        ArrayList<Integer> result = new ArrayList<>();
    
        while (number > 0) {
          int digit = (int) (number % 10);
          result.add(digit);
          number /= 10;
        }
        return result; // LSD at start of list
      }
    

    Testing each method by passing Long.MAX_VALUE 10,000,000 times, the string version took 2.090 seconds and the modulo version 2.334 seconds. (Oracle Java 8 on 64bit Ubuntu running in Eclipse Neon)

    So not a lot in it really, but I was a bit surprised that String was faster

    0 讨论(0)
提交回复
热议问题