Skip to content

Instantly share code, notes, and snippets.

@danzuep
Last active September 29, 2023 16:35
Show Gist options
  • Save danzuep/b4ed2964586c4ccef88ef3c16a4423bd to your computer and use it in GitHub Desktop.
Save danzuep/b4ed2964586c4ccef88ef3c16a4423bd to your computer and use it in GitHub Desktop.
public class JoinPointExample
{
private const int Max = 10000000;
private static int SumDigits(int num)
{
int sum = 0;
while (num > 0)
{
sum += num % 10;
num /= 10;
}
return sum;
}
public static int ComputeJoinPoint(int sequence1, int sequence2)
{
while (sequence1 != sequence2)
{
if (sequence1 >= Max || sequence2 >= Max)
return -1;
while (sequence1 < sequence2)
sequence1 += SumDigits(sequence1);
while (sequence2 < sequence1)
sequence2 += SumDigits(sequence2);
}
return sequence1;
}
public static void Test(int sequence1 = 471, int sequence2 = 480, int expectedResult = 519)
{
var result = ComputeJoinPoint(sequence1, sequence2);
System.Diagnostics.Debug.Assert(result == expectedResult);
Console.WriteLine($"The join point of {sequence1} and {sequence2} is: {result}");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment