Please Help me to convert this python code to C# #

avg_visits = [75, 30, 18, 17, 26, 40, 67, 82]
mixes = [0]*50001
mixes[0] = 1

for n in avg_visits:
    for i in range(0,50001):
        if (i+n <= 50000):
            mixes[i+n] = mixes[i]+mixes[i+n]

print mixes[500]
print mixes[2000]
print mixes[50000]

Show us what you've done so far.

This is all I have got. This was an answer for -

Patient Age Avg Visits / Year
<1 year 7.5
1-4 years 3.0
5-14 years 1.8
15-24 years 1.7
25-44 years 2.6
45-64 years 4.0
65-74 years 6.7
>75 years 8.2

The table above breaks out, by age range, the average number of visits people make to physicians each year. If you were an insurance company that wanted a member mix that would average 50 physician visits per year, you could sign up either of the following two member mixes:

  • 10 members in the "1-4 years" age range and
    5 members in the "45-64 years" age range
    resulting in (10 x 3.0) + (5 x 4.0) = 50

  • 2 members in the "<1 year" age range,
    10 members in the "5-14 years" age range and
    10 members in the "15-24 years" age range
    resulting in (2 x 7.5) + (10 x 1.8) + (10 x 1.7) = 50

Note that there are many more possibilities.

Question 1: What is the total number of different member mixes that result in 50 physician visits per year?
Question 2: What is the total number of different member mixes that result in 200 physician visits per year?
Question 3: What is the total number of different member mixes that result in 5000 physician visits per year?

I want it to write it in c#

What is your problem, why you do not write it simply directly in C#.

mixes[i+n] = mixes[i]+mixes[i+n]

could be expressed more concisely:

mixes[i+n] += mixes[i]

how to write

mixes = [0]*50001
mixes[0] = 1

to c#

The way you do array or list of 50001 values initial value zero, for zeroth element set value to 1. Should be easy, even in C#

commented: Thank you +0

Thank you

Here's some helpers:

# Python
avg_visits = [75, 30, 18, 17, 26, 40, 67, 82]

# C#
int[] avg_visits = { 75, 30, 18, 17, 26, 40, 67, 82 };

You can use for Python's list C#'s List<T>. That goes for mixes, like this:

# C#
List<BigInteger> mixes = new List<BigInteger>(50001);

And yes, you can use the BigInteger class if you're on >= .net framework 4.0, to get a more accurate response for mixes[5000] though it won't approach Python's number handling. I should mention that if you use BigInteger you'll have to manually add the Assembly reference System.Numerics and to add to your program the using System.Numerics; statement, otherwise you won't be able to use them. If you don't care for having a number with many digits, you can use the plain int thing instead of BigInteger.

To simulate Python's for n in avg_visits: you can use the foreach keyword, like this:

# C#
foreach (int n in avg_visits)
{
    #Insert code here
}

As for printing items to the screen (provided that you want a ConsoleApplication from C#, not a WindowsForms Application), you can use System.Console; like this:

# C#
Console.WriteLine(mixes[500]);
commented: Thank you +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.