0

Sum of numbers in prime indexes using c#

If the input is {2,3,10,15,11,19} then the output should be 44. We know that the prime numbers are 2,3,5,7 and so on. Here in index number 2 we have 10, 3 we have 15, 5 we have 19 Now the sum of those numbers is 10+15+19=44.

22nd May 2017, 11:40 AM
ranisanthoshi mattapalli
ranisanthoshi mattapalli - avatar
6 Answers
+ 15
int[] arr = {...} int size = arr.Length; First you'll have to generate the primes number from 0 to size (excluding) and store them in a list called primes. (you do this part yourself :D) int sum = 0; foreach(int p in primes) sum += arr[p]; Console.WriteLine(sum);
22nd May 2017, 12:12 PM
Jafca
Jafca - avatar
+ 11
So question solved? (if you want to generate primes, search for codes on SoloLearn or Google)
22nd May 2017, 12:18 PM
Jafca
Jafca - avatar
+ 2
Thanks for ur great advice... Already I did that
22nd May 2017, 12:32 PM
ranisanthoshi mattapalli
ranisanthoshi mattapalli - avatar
+ 1
I described it clearly na.... The numbers which are existing in the prime indexes... of that array should be added
22nd May 2017, 12:06 PM
ranisanthoshi mattapalli
ranisanthoshi mattapalli - avatar
+ 1
That is vat the logic I'm asking... I too know the thing you typed
22nd May 2017, 12:16 PM
ranisanthoshi mattapalli
ranisanthoshi mattapalli - avatar
0
A={2,3,10,15,11,19} A[2]=10, A[3] =15, A[5] =19.... add those numbers 10,15,19 and the output is 44 The index numbers 2,3,5 are primes.
22nd May 2017, 12:09 PM
ranisanthoshi mattapalli
ranisanthoshi mattapalli - avatar