How to detect when a persistence of a number reaches one digit

Viewed 489

I am trying to create a function that returns the persistence of a number, the main problem I believe is my do while loop at the bottom, I dont know how to get it to detect when there is one digit. The object is to iterate through using the nested function and increase the count with each iteration until the n equals a one digit number. The count is the numbers persistence, which is the number of times you must multiply the digits in the num until you reach a single digit. I expect 3 but I am getting the value of 2 instead.

    class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine(Persist.Persistence(39));
        Console.ReadLine();
    }
}

public class Persist
{
    public static int Persistence(long n)
    {
        int count = 0;
        if (n.ToString().Length == 1)
        {
            return count;
        }

        count = 1;
        //break up each number in the long individually.
        List<long> listofLong = new List<long>();
        while (n > 0)
        {
            listofLong.Add(n % 10);
            n = n / 10;
        }

        //First iteration of each number mult each other in list
        long calculate(List<long> seperatedNums)
        {
            long mult = 1;
            for (int i = 0; i < seperatedNums.Count; i++)
                mult *= seperatedNums[i];
            return (int)mult;
        }


        do
        {
            calculate(listofLong);
            count++;
        } while ((Math.Floor(Math.Log10(n)) + 1) > 1);

        return count;
    }
}

}

2 Answers

Well, single digit means 0..9 range; that's why it should be n > 9 or alike condition:

public static int Persistence(long n) {
  if (n < 0)
    throw new ArgumentOutOfRangeException(nameof(n));

  while (n > 9) {          // beyond a single digit
    long s = 1;

    for (; n > 0; n /= 10) // multiply all the digits
      s *= n % 10;

    n = s;
  }

  return (int)n;
}

Test:

// 2178 -> 2 * 7 * 1 * 8 = 112 -> 1 * 1 * 2 = 2
Console.Write(Persistence(2718));

In case we want to count loops:

public static int Persistence(long n) {
  if (n < 0)
    throw new ArgumentOutOfRangeException(nameof(n));

  int loops = 0;

  while (n > 9) {          // beyond a single digit
    long s = 1;

    for (; n > 0; n /= 10) // multiply all the digits
      s *= n % 10;

    n = s;
    loops += 1;
  }

  return loops;
}

Test:

// we have 3 steps here (39 -> 27 -> 14 -> 4): 
// 39 -> 3 * 9 = 27 -> 2 * 7 = 14 -> 1 * 4 = 4 
Console.Write(Persistence(39));

This would have to be the silliest code of have written in a while

public static long Persistence(long n)
{
   var i = 0;
   for (var s = n; s > 9; i++)
      do s *= n % 10; while ((n = n / 10) > 0);
   return i;
}

Or for more Printable Character OCD shenanigans

public static void Persistence(long n, ref long r)
{
   for (long s = n, i = 0; s > 9; r= ++i)
       do s *= n % 10; while ((n = n / 10) > 0);
}
Related