Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

C#

Michael Hajný
Michael Hajný
2,481 Points

Could you help me with convert 1000 digits to string and find largest product

how to read thousand numbers from user and In the 1000-digit number, find thirteen adjacent digits that have the largest product

6 Answers

Steven Parker
Steven Parker
229,732 Points

I'm guessing by "largest product", you mean when you take each digit as a separate number, right?

The obvious (or "brute force") way would be to split the digits into a list or array, and then use a loop to compute the product of each group of 13, scanning for the largest.

This won't take much effort for a computer to solve, but you could optimize it a bit by skipping ahead by 13 every time you encounter a zero (since the product of the next 13 groups will all be 0). Clearly, with this data most of the products will be 0 (769 of them, to be exact). :wink:

Give it a shot, and write again if you need more help (be sure to show your code).

Michael Hajný
Michael Hajný
2,481 Points

Do you have an idea how to write this code (if yes --> Could you help me with code) I don't know how. I have tried for two days.

And finaly thank you for your comment. I appreciate it a lot.

Steven Parker
Steven Parker
229,732 Points

Show the code from your last try to use as a starting point, and I can give you a few hints about the parts you're having trouble with.

Michael Hajný
Michael Hajný
2,481 Points

Console.WriteLine("Input Numbers:"); string number = Console.ReadLine(); string[] numberSA= number.Split(','); int[] numberIA = Array.ConvertAll<string, int>(numberSA, int.Parse);

        int soucin = 1;
        int nejvetsi = 0;
        for (int a = 0; a < 11; a++)
        {
            for(int i = 0; i < 14; i++)
            {
                soucin = soucin * numberIA[a + i];
            }
            if(soucin > nejvetsi)
            {
                nejvetsi = soucin;
            }
        }
        Console.WriteLine(soucin);


        Console.WriteLine(numberIA);

I don´t know what now. Where is the mistake. Could you help me with repare.

Michael Hajný
Michael Hajný
2,481 Points

13980247870379103690179014337490132397194930172019 86983520312774506326239578318016984801869478851843 75861560789112949495459501737958331952853208805511 02540698747158523863050715693290963295227443043557 86696648950445244523161731856403098711121722383113 52229893423380308135336276614282806444486645238749 20358907296290491560440772390713810515859307960866 80172427121883998797908792274921901699720888093776 75727333001053367881220235421809751254540594752243 62584907711670556013604839586446706324415722155397 43697817977846174064955149290862569321978468622482 33972241375657056057490261407972968652414535100474 42166370484403199890008895243450658541227588666881 26427171479924442928230863465674813919123162824586 07866458359124566529476545682848912883142607690042 14219022671055626321111109370544217506941658960408 17198403850962455444362981230987879927244284909188 74580156166097919133875499200524063689912560717606 35886116467109405077541002256983155200055935729725 21636269561882670428252483600823257530420752963450 between those numbers

Michael Hajný
Michael Hajný
2,481 Points

There is a final version

static void Main(string[] args) { //součin = product //nejvěší = gradests Console.WriteLine("Input Numbers:"); string number = Console.ReadLine(); string[] numberSA = number.Split(); Console.WriteLine(numberSA); Console.ReadKey(); int[] numberIA = Array.ConvertAll<string, int>(numberSA, int.Parse); int product = 1; int gradests = 0;

        for (int a = 0; a < 1001; a++)
        {
            for(int i = 0; i < 14; i++)
            {
                product = product * numberIA[a + i];
            }
            if (product > gradests)
            {
                gradests = product;
            }
        }
        Console.WriteLine(product);
        Console.WriteLine(numberIA);
        Console.ReadKey();
    }
Steven Parker
Steven Parker
229,732 Points

Looks like you're getting close, but I'd guess this isn't quite working yet. Some things you may want to change:

  • use "ToCharArray" instead of "Split"
  • alter the conversion to work with characters instead of strings (to go with the char array)
  • use the actual length instead of a fixed number to control the loop
  • shouldn't the inner loop run 13 times instead of 14?
  • reset the product to 1 inside the outer loop
  • show the max value instead of the last product at the end
Michael Hajný
Michael Hajný
2,481 Points

Thank you Steven. I appraised that. I will try it :"))You were right with loop and his repetitions thank you again.

Michael Hajný
Michael Hajný
2,481 Points

How can i change the conversion on to characters instead of strings ?

Steven Parker
Steven Parker
229,732 Points

An easy way to convert a digit character into a number is to just subtract the value of '0':

    int[] numberIA = Array.ConvertAll<char, int>(numberSA, d => d - '0');
Michael Hajný
Michael Hajný
2,481 Points

Thanks for everything I learned a lot :")