如何检测数字的持久性何时达到一位数
How to detect when a persistence of a number reaches one digit
我正在尝试创建一个 returns 数字持久性的函数,我认为主要问题是底部的 do while 循环,我不知道如何让它检测何时有一个数字。目标是使用嵌套函数进行迭代,并在每次迭代时增加计数,直到 n 等于一位数。计数是数字持久性,这是您必须将 num 中的数字相乘直到达到一位数的次数。我期望 3 但我得到的是 2 的值。
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;
}
}
}
嗯,个位数表示0..9
范围;这就是为什么它应该是 n > 9
或类似的条件:
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;
}
测试:
// 2178 -> 2 * 7 * 1 * 8 = 112 -> 1 * 1 * 2 = 2
Console.Write(Persistence(2718));
万一我们要数 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;
}
测试:
// we have 3 steps here (39 -> 27 -> 14 -> 4):
// 39 -> 3 * 9 = 27 -> 2 * 7 = 14 -> 1 * 4 = 4
Console.Write(Persistence(39));
这一定是最近写的最愚蠢的代码了
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;
}
或更多可打印字符强迫症恶作剧
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);
}
我正在尝试创建一个 returns 数字持久性的函数,我认为主要问题是底部的 do while 循环,我不知道如何让它检测何时有一个数字。目标是使用嵌套函数进行迭代,并在每次迭代时增加计数,直到 n 等于一位数。计数是数字持久性,这是您必须将 num 中的数字相乘直到达到一位数的次数。我期望 3 但我得到的是 2 的值。
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;
}
}
}
嗯,个位数表示0..9
范围;这就是为什么它应该是 n > 9
或类似的条件:
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;
}
测试:
// 2178 -> 2 * 7 * 1 * 8 = 112 -> 1 * 1 * 2 = 2
Console.Write(Persistence(2718));
万一我们要数 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;
}
测试:
// we have 3 steps here (39 -> 27 -> 14 -> 4):
// 39 -> 3 * 9 = 27 -> 2 * 7 = 14 -> 1 * 4 = 4
Console.Write(Persistence(39));
这一定是最近写的最愚蠢的代码了
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;
}
或更多可打印字符强迫症恶作剧
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);
}