My multithread creates or overwrites an extra/existing thread - c#

I want to spread a test over multiple threads, so first I divide the total numbers I want tested equally over the amount of threads I want (including remainder). Then I assign the testing that range of numbers to each thread. Everytime the test is positive, a counter is incremented. I test it over a range from 0 to 123, as I know what the result should be, but whenever I assign more then 1 thread to the task, I get the wrong result. When debugging, I noticed that after the starting of the threads, the current line skips back to assigning a new thread. I don't understand why. I have the counter protected by a lock, which as far as i know is working properly. Here is the corresponding code:
for (int n = 0; n < remainder; n++)
{
workers[n] = new Thread(() => CountNumbers(startvalue + n * (tasks + 1), startvalue + (n + 1) * (tasks + 1), modulus));
}
for (int m = remainder; m < nrthreads; m++)
{
workers[m] = new Thread(() => CountNumbers(startvalue + remainder * (tasks + 1) + (m - remainder) * tasks, startvalue + remainder * (tasks + 1) + (m - remainder + 1) * tasks, modulus));
}
for (int k = 0; k < nrthreads; k++)
{
workers[k].Start();
}
for (int k = 0; k < nrthreads; k++)
{
workers[k].Join();
}
the "problem" arises when workers[k].Start() is finished for all k, then it for some reason overwrites the last thread in workers.
I'm relative new to C#, so I struggle with finding the flaw. It is for school, so hints in the right direction are probably more appropiate then clean answers.

This is a common error in C# parallel programming. When you declare anonymous functions through lambda expressions, they capture any variables (not values) that they reference. In your case, all your threads are capturing the same variable instance for the n or m counter, leading all their executions to see its last value.
A simple way to resolve this is to declare another variable within the scope of your loop, and copy the counter to it. Since the scope is restricted to the loop, the variable will not be shared across your threads.
for (int nOuter = 0; nOuter < remainder; nOuter++)
{
int n = nOuter;
workers[n] = new Thread(() => CountNumbers(startvalue + n * (tasks + 1), startvalue + (n + 1) * (tasks + 1), modulus));
}
for (int mOuter = remainder; mOuter < nrthreads; mOuter++)
{
int m = mOuter;
workers[m] = new Thread(() => CountNumbers(startvalue + remainder * (tasks + 1) + (m - remainder) * tasks, startvalue + remainder * (tasks + 1) + (m - remainder + 1) * tasks, modulus));
}
Edit: You can simplify your code if you switch to using PLINQ or TPL constructs. The following should be equivalent to your entire logic:
Parallel.For(0, nrthreads, k =>
{
if (k < remainder)
CountNumbers(startvalue + k * (tasks + 1), startvalue + (k + 1) * (tasks + 1), modulus);
else
CountNumbers(startvalue + remainder * (tasks + 1) + (k - remainder) * tasks, startvalue + remainder * (tasks + 1) + (k - remainder + 1) * tasks, modulus);
});

Your missing a lock on something because you are out of sync. I had the same kind of issue once before only I was iterating a list and I had a valid index value (impossible to be out of range when you use list.Count) yet it was. Because I did not have a proper lock.
The issue is probably in your CountNumbers Method.

Related

Is there a good way to optimise a nested for loop that is multiplying the iterations?

I have a for loop that is creates a sum by multiplying the values in the loop:
int i;
int j;
float total = 0;
int x = 1000;
for (i = 0; i < x; i++)
{
for (j = 0; j < x; j++)
{
total += i * j;
}
}
Is there a better way of writing this? When x gets larger, the time it takes to process is incredibly long.
My research suggests that nested for loops are good enough, but I'm wondering if there's a way to simplify this.
You don't want nested loops at all, the closed formula for given x is
total = x * x * (x - 1) * (x - 1) / 4;
Just be careful with integer overflow (note long for both the result and for x)
long x = 1000;
long total = x * x * (x - 1) * (x - 1) / 4;
Edit: let's have a look at general case, where we have to work with arbitrary numbers, say, arrays a and b:
long total = 0;
for (int i = 0; i < a.Length; ++i)
for (int j = 0; j < b.Length; ++j)
total += a[i] * b[j];
We can't propose closed formula in this case, but we can optimize the computation. We are looking for the total of
a[0] * b[0] + a[0] * b[1] + ... + a[0] * b[m] +
a[1] * b[0] + a[1] * b[1] + ... + a[1] * b[m] +
...
a[n] * b[0] + a[n] * b[1] + ... + a[n] * b[m] =
= a[0] * (b[0] + b[1] + ... + b[m]) +
a[1] * (b[0] + b[1] + ... + b[m]) +
...
a[n] * (b[0] + b[1] + ... + b[m]) =
= (a[0] + a[1] + ... a[n]) * (b[0] + b[1] + ... + b[m])
So far so good, instead of nested loops and O(n * m) complexity we have two separated loops and just O(n + m) time complexity.
Often we do such tasks with a help of Linq:
using System.Linq;
...
long total = a.Sum(item => (long) item) * b.Sum(item => (long) item)
But you can well use good old loops:
long sumA = 0;
foreach (var item in a)
sumA += item;
long sumB = 0;
foreach (var item in b)
sumB += item;
long total = sumA * sumB;
For your specific case, there is of course a constant-time expression (see Dmitry's answer). For a more general case, you can use
Σi Σj Xi.Yj = (Σi Xi).(Σj Yi)
This turns O(N²) to O(N).

Going from Parallel.ForEach to Multithreading

So I converted a recursive function to iterative and then used Parallel.ForEach but when I was running it through VTune it was only really using 2 logical cores at for the majority of its run time.
I decided to attempt to use managed threads instead, and converted this code:
for (int N = 2; N <= length; N <<= 1)
{
int maxThreads = 4;
var workGroup = Enumerable.Range(0, maxThreads);
Parallel.ForEach(workGroup, i =>
{
for (int j = ((i / maxThreads) * length); j < (((i + 1) / maxThreads) * length); j += N)
{
for (int k = 0; k < N / 2; k++)
{
int evenIndex = j + k;
int oddIndex = j + k + (N / 2);
var even = output[evenIndex];
var odd = output[oddIndex];
output[evenIndex] = even + odd * twiddles[k * (length / N)];
output[oddIndex] = even + odd * twiddles[(k + (N / 2)) * (length / N)];
}
}
});
}
Into this:
for (int N = 2; N <= length; N <<= 1)
{
int maxThreads = 4;
Thread one = new Thread(() => calculateChunk(0, maxThreads, length, N, output));
Thread two = new Thread(() => calculateChunk(1, maxThreads, length, N, output));
Thread three = new Thread(() => calculateChunk(2, maxThreads, length, N, output));
Thread four = new Thread(() => calculateChunk(3, maxThreads, length, N, output));
one.Start();
two.Start();
three.Start();
four.Start();
}
public void calculateChunk(int i, int maxThreads, int length, int N, Complex[] output)
{
for (int j = ((i / maxThreads) * length); j < (((i + 1) / maxThreads) * length); j += N)
{
for (int k = 0; k < N / 2; k++)
{
int evenIndex = j + k;
int oddIndex = j + k + (N / 2);
var even = output[evenIndex];
var odd = output[oddIndex];
output[evenIndex] = even + odd * twiddles[k * (length / N)];
output[oddIndex] = even + odd * twiddles[(k + (N / 2)) * (length / N)];
}
}
}
The issue is in the fourth thread on the last iteration of the N loop I get a index out of bounds exception for the output array where the index is attempting access the equivalent of the length.
I can not pinpoint the cause using debugging, but I believe it is to do with the threads, I ran the code without the threads and it worked as intended.
If any of the code needs changing let me know, I usually have a few people suggest edits. Thanks for your help, I have tried to sort it myself and am fairly certain the problem is occurring in my threading but I can not see how.
PS: The intended purpose is to parallelize this segment of code.
The observed behaviour is almost certainly due to the use of a captured loop iteration variable N. I can reproduce your situation with a simple test:
ConcurrentBag<int> numbers = new ConcurrentBag<int>();
for (int i = 0; i < 10000; i++)
{
Thread t = new Thread(() => numbers.Add(i));
t.Start();
//t.Join(); // Uncomment this to get expected behaviour.
}
// You'd not expect this assert to be true, but most of the time it will be.
Assert.True(numbers.Contains(10000));
Put simply, your for loop is racing to increment N before the value of N can be copied by the delegate that executes the calculateChunk call. As a result calculateChunk sees almost random values of N going up to (and including) length <<= 1 - that's what's causing your IndexOutOfRangeException.
The output values you'll get will be rubbish too as you can never rely on the value of N being correct.
If you want to safely rewrite the original code to utilize more cores, move Parallel.ForEach from the inner loop to the outer loop. If the number of outer loop iterations is high, the load balancer will be able to do its job properly (which it can't with your current workGroup count of 4 - that number of elements is simply too low).

Performance issues with algorithm for calculating the average of an array in .NET

The following code creates an average array of 200,000 arrays each with 512 elements.
void Main()
{
double[] avg = new double[512];
int start = System.Environment.TickCount;
for (int i = 0; i < 200000; i++)
{
for (int j = 0; j < avg.Length; j++)
{
// The `i` in `i-avg[j]` is a dummy for the measured variable.
avg[j] = avg[j] + (i - avg[j])/(i + 1);
}
}
Console.WriteLine(System.Environment.TickCount-start);
}
The reason for the iterative average is to avoid overflow when summing the values of 200,000 arrays.
In the real world, the 200,000 arrays are DFT's which are generated in 250 ms using the FFTW library. I am a little surprised that on my system calculating the average array takes about 500-600 ms (on average) i.e. iteration and a few flops takes 2-3 times longer that doing FFT.
Is there a way to speed it up or accomplish the same result using a different (faster) way in .NET, or do I have to switch languages for increased speed?
You'll save a little bit by unrolling the inner loop by a divisor of 512. Also I'm not sure I understand what the i loop is for, as it stands the (i+1) can be pre-calculated and replaced with a multiplication. Is there a way to apply the same optimization in your real-world case, or are you doing that just to improve the accuracy of the timing?
for (double i = 0; i < 200000; i++)
{
var inv_i_plus_1 = 1.0 / (i + 1);
for (int j = 0; j < avg.Length; )
{
// The `i` in `i-avg[j]` is a dummy for the measured variable.
avg[j] = avg[j] + (i - avg[j]) * inv_i_plus_1; j++;
avg[j] = avg[j] + (i - avg[j]) * inv_i_plus_1; j++;
avg[j] = avg[j] + (i - avg[j]) * inv_i_plus_1; j++;
avg[j] = avg[j] + (i - avg[j]) * inv_i_plus_1; j++;
avg[j] = avg[j] + (i - avg[j]) * inv_i_plus_1; j++;
avg[j] = avg[j] + (i - avg[j]) * inv_i_plus_1; j++;
avg[j] = avg[j] + (i - avg[j]) * inv_i_plus_1; j++;
avg[j] = avg[j] + (i - avg[j]) * inv_i_plus_1; j++;
}
}
The expression
avg[j] + (i - avg[j]) / (i + 1)
after simplifying becomes
(avg[j] + 1) * i / (i + 1)
As we can see, i / (i + 1) has nothing in common with j, so we can precalculate it outside of the inner loop
for (int i = 0; i < 200000; i++)
{
double k = i;
k = k / (k + 1);
for (int j = 0; j < avg.Length; j++)
{
avg[j] = (avg[j] + 1) * k;
}
}
The final code is about 4 to 6 times faster than the original.
The main reason must be the division in the inner loop.
You may sacrifice a little of accuracy and do a straight summation. There is no risk of overflow with doubles.
If by any chance the values are sorted in increasing or decreasing order (or close to that), summing in increasing order will preserve good accuracy.
Just change the division to a reciprocal multiplication and then hoist the reciprocal calculation out of the loop:
void Main()
{
double[] avg = new double[512];
int start = System.Environment.TickCount;
for (int i = 0; i < 200000; i++)
{
double di = (double)i;
double r = 1/(di + 1);
for (int j = 0; j < avg.Length; j++)
{
// The `i` in `i-avg[j]` is a dummy for the measured variable.
avg[j] = avg[j] + (di - avg[j]) * r;
}
}
Console.WriteLine(System.Environment.TickCount-start);
}

Get sum of each digit below n as long

This is the code I have but it's to slow, any way to do it faster..
the number range I'm hitting is 123456789 but I can't get it below 15 seconds, and I need it to get below 5 seconds..
long num = 0;
for (long i = 0; i <= n; i++)
{
num = num + GetSumOfDigits(i);
}
static long GetSumOfDigits(long n)
{
long num2 = 0;
long num3 = n;
long r = 0;
while (num3 != 0)
{
r = num3 % 10;
num3 = num3 / 10;
num2 = num2 + r;
}
return num2;
}
sum =(n(n+1))/2 is not giving me the results I need, not calculating properly..
For N = 12 the sum is 1+2+3+4+5+6+7+8+9+(1+0)+(1+1)+(1+2)= 51.
I need to do this with a formula instead of a loop..
I've got about 15 tests to run through each under 6 seconds..
with parallel I got one test from 15seconds to 4-8 seconds..
Just to show you the test I'm doing this is the hard one..
[Test]
public void When123456789_Then4366712385()
{
Assert.AreEqual(4366712385, TwistedSum.Solution(123456789));
}
On my computer I can run all the tests under 5 seconds..
Look at the photo..
With DineMartine Answer I got these results:
Your algorithm complexity is N log(N). I have found a better algorithm with complexity log(N). The idea is to iterate on the number of digits which is :
log10(n) = ln(n)/ln(10) = O(log(n)).
The demonstration of this algorithm involves a lot of combinatorial calculus. So I choose not to write it here.
Here is the code :
public static long Resolve(long input)
{
var n = (long)Math.Log10(input);
var tenPow = (long)Math.Pow(10, n);
var rest = input;
var result = 0L;
for (; n > 0; n--)
{
var dn = rest / tenPow;
rest = rest - dn * tenPow;
tenPow = tenPow / 10;
result += dn * (rest + 1) + dn * 45 * n * tenPow + dn * (dn-1) * tenPow * 5 ;
}
result += rest * (rest + 1) / 2;
return result;
}
Now you would solve the problem in a fraction of second.
The idea is to write the input as a list of digit :
Assuming the solution is given by a function f, we are looking for g a recursive expression of f over n :
Actually g can be written as follow :
If you find h, the problem would be practically solved.
A little bit convoluted but gets the time down to practicaly zero:
private static long getSumOfSumOfDigitsBelow(long num)
{
if (num == 0)
return 0;
// 1 -> 1 ; 12 -> 10; 123 -> 100; 321 -> 100, ...
int pow10 = (int)Math.Pow(10, Math.Floor(Math.Log10(num)));
long firstDigit = num / pow10;
long sum = 0;
var sum999 = getSumOfSumOfDigitsBelow(pow10 - 1);
var sumRest = getSumOfSumOfDigitsBelow(num % pow10);
sum += (firstDigit - 1)*(firstDigit - 0)/2*pow10 + firstDigit*sum999;
sum += firstDigit*(num%pow10 + 1) + sumRest;
return sum;
}
getSumOfSumOfDigitsBelow(123456789) -> 4366712385 (80us)
getSumOfSumOfDigitsBelow(9223372036854775807) -> 6885105964130132360 (500us - unverified)
The trick is to avoid to compute the same answer again and again. e.g. 33:
your approach:
sum = 1+2+3+4+5+6+7+8+9+(1+0)+(1+1)+(1+2)+ ... +(3+2)+(3+3)
my approach:
sum = 10*(0 + (1+2+3+4+5+6+7+8+9)) +
10*(1 + (1+2+3+4+5+6+7+8+9)) +
10*(2 + (1+2+3+4+5+6+7+8+9)) +
3*(3 + (1 + 2 + 3))
The (1+2+3+4+5+6+7+8+9)-part have to be calculated only once. The loop of 0..firstDigit-1 can be avoided by the n(n-1)/2-trick. I hope this makes sense.
The complexity is O(2^N) with N counting the number of digits. This looks very bad but is fast enough for your threshold of 5s even for long-max. It may be possible to transform this algorithm in something running in O(n) by calling getSumOfSumOfDigitsBelow() only once but it would look much more complex.
First step of optimization: look at your algorithm ;)
Comming back to this problem after the answer of DineMartine :
To further optimize the algorithm, the sum999-part can be replaced by an explicit formula. Lets take some number 9999...9=10^k-1 into the code and replace accordingly:
sum(10^k-1) = (9 - 1)*(9 - 0)/2*pow10 + 9*sum999 + 9*(num%pow10 + 1) + sumRest
sum(10^k-1) = 36*pow10 + 9*sum999 + 9*(num%pow10 + 1) + sumRest
sum999 and sumRest are the same for the numbers of type 10^k:
sum(10^k-1) = 36*pow10 + 10*sum(10^(k-1)-1) + 9*(num%pow10 + 1)
sum(10^k-1) = 36*pow10 + 10*sum(10^(k-1)-1) + 9*((10^k-1)%pow10 + 1)
sum(10^k-1) = 36*pow10 + 10*sum(10^(k-1)-1) + 9*pow10
sum(10^k-1) = 45*pow10 + 10*sum(10^(k-1)-1)
We have a definition of sum(10^k-1) and know sum(9)=45. And we get:
sum(10^k-1) = 45*k*10^k
The updated code:
private static long getSumOfSumOfDigitsBelow(long num)
{
if (num == 0)
return 0;
long N = (int) Math.Floor(Math.Log10(num));
int pow10 = (int)Math.Pow(10, N);
long firstDigit = num / pow10;
long sum = (firstDigit - 1)*firstDigit/2*pow10
+ firstDigit* 45 * N * pow10 / 10
+ firstDigit*(num%pow10 + 1)
+ getSumOfSumOfDigitsBelow(num % pow10);
return sum;
}
This is the same algorithm as the one from DineMartine but expressed in a recursive fashion (I've compared both implementations and yes I'm sure it is ;) ). The runtime goes down to practically zero and the time complexity is O(N) counting the number of digits or O(long(N)) taking the value.
If you have multiple processors (or cores) in your system, you can speed it up quite a lot by doing the calculations in parallel.
The following code demonstrates (it's a compilable console app).
The output when I try it on my system (4 cores with hyperthreading) is as follows for a release build:
x86 version:
Serial took: 00:00:14.6890714
Parallel took: 00:00:03.5324480
Linq took: 00:00:04.4480217
Fast Parallel took: 00:00:01.6371894
x64 version:
Serial took: 00:00:05.1424354
Parallel took: 00:00:00.9860272
Linq took: 00:00:02.6912356
Fast Parallel took: 00:00:00.4154711
Note that the parallel version is around 4 times faster. Also note that the x64 version is MUCH faster (due to the use of long in the calculations).
The code uses Parallel.ForEach along with a Partitioner to split the range of number into sensible regions for the number of processors available. It also uses Interlocked.Add() to quickly add the numbers with efficient locking.
I've also added another method where you need to pre-calculate the sums for the numbers between 0 and 1000. You should only need to pre-calculate the sums once for each run of the program. See FastGetSumOfDigits().
Using FastGetSumOfDigits() more than doubles the previous fastest time on my PC. You can increase the value of SUMS_SIZE to a larger multiple of 10 to increase the speed still further, at the expense of space. Increasing it to 10000 on my PC decreased the time to ~0.3s
(The sums array only needs to be a short array, to save space. It doesn't need a larger type.)
using System;
using System.Collections.Concurrent;
using System.Diagnostics;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
namespace Demo
{
internal class Program
{
public static void Main()
{
long n = 123456789;
Stopwatch sw = Stopwatch.StartNew();
long num = 0;
for (long i = 0; i <= n; i++)
num = num + GetSumOfDigits(i);
Console.WriteLine("Serial took: " + sw.Elapsed);
Console.WriteLine(num);
sw.Restart();
num = 0;
var rangePartitioner = Partitioner.Create(0, n + 1);
Parallel.ForEach(rangePartitioner, (range, loopState) =>
{
long subtotal = 0;
for (long i = range.Item1; i < range.Item2; i++)
subtotal += GetSumOfDigits(i);
Interlocked.Add(ref num, subtotal);
});
Console.WriteLine("Parallel took: " + sw.Elapsed);
Console.WriteLine(num);
sw.Restart();
num = Enumerable.Range(1, 123456789).AsParallel().Select(i => GetSumOfDigits(i)).Sum();
Console.WriteLine("Linq took: " + sw.Elapsed);
Console.WriteLine(num);
sw.Restart();
initSums();
num = 0;
Parallel.ForEach(rangePartitioner, (range, loopState) =>
{
long subtotal = 0;
for (long i = range.Item1; i < range.Item2; i++)
subtotal += FastGetSumOfDigits(i);
Interlocked.Add(ref num, subtotal);
});
Console.WriteLine("Fast Parallel took: " + sw.Elapsed);
Console.WriteLine(num);
}
private static void initSums()
{
for (int i = 0; i < SUMS_SIZE; ++i)
sums[i] = (short)GetSumOfDigits(i);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static long GetSumOfDigits(long n)
{
long sum = 0;
while (n != 0)
{
sum += n%10;
n /= 10;
}
return sum;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static long FastGetSumOfDigits(long n)
{
long sum = 0;
while (n != 0)
{
sum += sums[n % SUMS_SIZE];
n /= SUMS_SIZE;
}
return sum;
}
static short[] sums = new short[SUMS_SIZE];
private const int SUMS_SIZE = 1000;
}
}
To increase performance you could calculate the sum starting from the highest number.
Let r=n%10 + 1. Calculate the sum for the last r numbers.
Then we note that if n ends with a 9, then the total sum can be calculated as 10 * sum(n/10) + (n+1)/10 * 45. The first term is the sum of all digits but the last, and the second term is the sum of the last digit.
The function to calculate the total sum becomes:
static long GetSumDigitFrom1toN(long n)
{
long num2 = 0;
long i;
long r = n%10 + 1;
if (n <= 0)
{
return 0;
}
for (i = 0; i < r; i++)
{
num2 += GetSumOfDigits(n - i);
}
// The magic number 45 is the sum of 1 to 9.
return num2 + 10 * GetSumDigitFrom1toN(n/10 - 1) + (n/10) * 45;
}
Test run:
GetSumDigitFrom1toN(12L): 51
GetSumDigitFrom1toN(123456789L): 4366712385
The time complexity is O(log n).
Sum of digits for 0..99999999 is 10000000 * 8 * (0 + 1 + 2 + ... + 9).
Then calculating the rest (100000000..123456789) using the loop might be fast enough.
For N = 12:
Sum of digits for 0..9 is 1 * 1 * 45. Then use your loop for 10, 11, 12.
For N = 123:
Sum of digits for 0..99 is 10 * 2 * 45. Then use your loop for 100..123.
You see the pattern?
A different approach you can try:
Convert the number to a string, then to a Char array
Sum the ASCII codes for all the chars, minus the code for 0
Example code:
long num = 123456789;
var numChars = num.ToString().ToCharArray();
var zeroCode = Convert.ToByte('0');
var sum = numChars.Sum(ch => Convert.ToByte(ch) - zeroCode);

Is there a possible loop for this?

I just started to learn C# and only know really basic stuff. So this question may be easy to you, but very hard to me. The more detail in your answer, the better.
The next line of code will check if a studentnumber is real or fake. The number is real when the sum of all the characters (when multiplied by their place number) are a multiple of 11.
Console.WriteLine("What is your studentnumber?");
stnum = Console.ReadLine();
var stnumint = Convert.ToInt32(stnum);
var ans1 = (stnumint % 10 - stnumint % 1) / 1;
var ans2 = (stnumint % 100 - stnumint % 10) / 10;
var ans3 = (stnumint % 1000 - stnumint % 100) / 100;
var ans4 = (stnumint % 10000 - stnumint % 1000) / 1000;
var ans5 = (stnumint % 100000 - stnumint % 10000) / 10000;
var ans6 = (stnumint % 1000000 - stnumint % 100000) / 100000;
var ans7 = (stnumint % 10000000 - stnumint % 1000000) / 1000000;
var control = ans1 * 1 + ans2 * 2 + ans3 * 3 + ans4 * 4 + ans5 * 5 + ans6 * 6 + ans7 * 7;
var endnum = control % 11;
if (endnum == 0)
{
Console.WriteLine("You have got a valid studentnumber.");
}
else
{
Console.WriteLine("Check if your studentnumber is correct. If it is, you are not a student.");
}
Take for example studentnumber 9232753. When calculating: (9*7 + 2*6 + 3*5 + 2*4 + 7*3 + 5*2 + 3*1) % 11, the answer will be 0.
How can I write this line of code into a smaller loop?
One equivalent loop would be:
int control = 0;
int power10 = 1; // var to save an expensive `Math.Power` call
for (int i = 1; i <= 7; i++)
{
control += ((stnumint % (power10*10) - stnumint % power10) / power10) * i;
power10 *= 10;
}
I would highly recommend not using var for built-in types like int, string, etc. You leave the resulting type at the mercy of the compiler which can give you unexpected results. Save var for when the actual type is difficult (or impossible) to determine at design-time.
var totalAns = 0;
for (int i = 1; i <= 10000000; i *= 10)
{
ans += (stnumint % (10*i) - stnumint % i) / i;
}
Here is the part for calculation. If you need to save stnumint, copy it another variable.
int stnumint=...; //must be sure, that data type is n
int checksum=0;
int i=1;
while(stnumint>0)
{
checksum=(stnumint%10)*i;
i++;
//in real numbers will look like floor(stnumint/10)
//will automaticly floor because of data type int
stnumint=stnumint/10;
}

Categories

Resources