Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find number on a certain position in array, depending on some rules, added plural to "numbers"

I am a beginner to programming and I am attempting to write a program that should solve the following mathematical problem:

"On a circle we have 108 numbers. The sum of any 20 consecutive numbers is always 1000. At position "1" we have the number 1, at position "19" we have the number 19 and at position "50" we have number 50. What number do we have on position "100"?" The numbers are always integers.

Up to this point i know how to use array and looping structures like "for".

I attempted to set a variable "check" which I initialized it as "false" in order to change it later to true, when I will find the number on position "100".

My initial thought is to go through the array and make the sum of each 20 consecutive numbers. I used two variables in order to assign values for each position that is not 1, 19 or 50 and then make the sum. I did this in two "for" structures, one inside another. Of course, this creates an issue that i am not able to properly calculate the values of the first 20 number.

I checked on the MSDN some information about the "Queue" structure, yet I can't understand how to use it in my particular case.

Let me know please if anyone has some suggestions on how i should handle this. Thank you.

As you asked, here is the code that I have managed to write so far. Please take into consideration that I am really in the begging of programming and I still have a lot to learn.

check = true;
int[] array = new int[108];

for (counter = 1; counter <= array.Length; counter++)
    {
        if (counter !=1 || counter !=19 || counter !=50)
        {
            for (i = 1; i <= 999; i++) 
            {
                array[counter] = i;
                if (counter >= 20) 
                {
                    for (consecutive = counter; consecutive => 2; consecutive--)
                    {
                        checkValue = array[consecutive] + array[consecutive]-1;
                    }
                    if (checkvalue != 1000) check = false;
                }
            }
        }
    }
like image 849
Pedro Delarte Avatar asked Jan 28 '26 01:01

Pedro Delarte


2 Answers

As say @fryday the code of this problem doesn't mean anything without logic, so you should understand well the solution before read this.

Let me explain to you the logic of this problem and then read the code (you will see that is quite simple). First you may note that each time you get 20 consecutive numbers, the next number must be equal to the first in the 20 consecutive, because you have

x_1 + x_2 + ... + x_20 = 1000

and

x_2 + ... + x_20 + x_21 = 1000

so

x_1 + x_2 + ... + x_20 = x_2 + ... + x_20 + x_21

and finally

x_1 = x_21

Following this insight all we have to do is walk through the array starting at each one of this positions 1, 19 and 50, fill the positions in the array with this values and then calculate the next position adding 20 to the current and mod by 108, repeat the process until the value in the array is equal to 1, 19 or 50 in each case. Then the values in the array equals to 0 means that all must have the same value (and has not found yet). To found this value sum 20 values consecutive and divide the difference with 1000 by the count of elements with value equal to 0 in such 20 consecutive. You can notice that the position 100 is one of this that have value equals to 0. Finally we get the value of 130.

This is the code:

using System;

namespace Stackoverflow
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] values = { 1, 19, 50 };
            int[] arr = new int[108];

            foreach (var val in values)
            {
                int pos = val - 1;
                while (arr[pos] != val)
                {
                    arr[pos] = val;

                    pos += 20;
                    pos %= 108;
                }
            }

            int sum = 0;
            int zeros = 0;
            for (int i = 0; i < 20; i++)
            {
                if (arr[i] == 0) zeros++;
                sum += arr[i];
            }

            Console.WriteLine((1000 - sum) / zeros);
        }
    }
}
like image 98
leobelizquierdo Avatar answered Jan 30 '26 14:01

leobelizquierdo


I solved this problem with array, but better use some type of cycle list. It's about structure choice.

In general you should solve this problem with logic, computer only can help you to check result or visualize data. I'll append code which show how I use array for solving this problem:

N = 108
step = 20
a = [None] * (N + 1)
def toArrayIndex(i):
    if i < 0:
        return toArrayIndex(N + i)
    if i % N == 0:
        return N
    else:
        return i % N


def setKnownNumbers(value, index):
    while a[toArrayIndex(index)] != value:
        if a[toArrayIndex(index)] != None:
            print "ERROR: problem unsolvable i = %d" % toArrayIndex(index)
        a[toArrayIndex(index)] = value
        index += step

setKnownNumbers(1, 1)
setKnownNumbers(19, 19)
setKnownNumbers(50, 50)

Now you only need to understand how finish it. It's not hard ;)

You can use thi code to output array and check correctness

# output 
for i in range(1,N + 1):
    print i, a[i]

counter = 0
for i in range(1,N + 1):
    s = 0
    for j in range(0,step):
        s += a[toArrayIndex(i + j)]
    if s == 1000:
        counter += 1

if counter == N:
    print "Correct"
print "a[100] = %d" % a[100]
like image 38
fryday Avatar answered Jan 30 '26 15:01

fryday



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!