computing 13 digit number get freezes

im answering one of the problem in project euler

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <fstream>
#include <iostream>
#include <string>


unsigned long long getProduct(unsigned long number)
{
   unsigned long long trailingzeroes = 10;
   unsigned  long long product = 1;

    for ( unsigned long long a = 2; a < number*2; a*=10, trailingzeroes*=10 )
    {
            unsigned long long ctr = 0;
            while( number%trailingzeroes != 0 )
            {
                --number;
                ++ctr;
            }
            ctr /= (trailingzeroes/10);

            product*=ctr;

    }
    return product;
}
int main()
{
    std::ios_base::sync_with_stdio(false);
    std::ifstream file("number.txt");
    std::string file_content;

    if ( file )
    {

        char byte;
        while(!file.eof())
        {
            file.get(byte);
            if(byte!='\n'&&byte!='#')
                file_content += byte;
        }

    }

   unsigned long long highest = 1;
   unsigned long long product = 0;
   unsigned long long number = 1;

    for ( int a = 0; a < file_content.size()-13; a++ )
    {
        for ( int b = a; b < a+13; b++ )
        {
            number*=10;
            number += file_content[b] - 48;
        }
        std::cout << number;

        product = getProduct(number);
        if (  product > highest )
        {
            highest = product;
        }
        number = 1;

    }
    std::cout << highest;
}


this code gets freezes like very very very slow, but when i decrease the number to compute( like 6 or 7 digits only) it works well.

the getProduct function gets the product of every digit in a number
(i.e., 654 = 6x5x4 = 120 )

Last edited on
Does it freeze (stop doing anything) or does it just take a long time?
heres the minimal compilable code:

this first snippet freezes:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
unsigned long long getProduct(unsigned long number)
{
   unsigned long long trailingzeroes = 10;
   unsigned  long long product = 1;

    for ( unsigned long long a = 2; a < number*2; a*=10, trailingzeroes*=10 )
    {
            unsigned long long ctr = 0;
            while( number%trailingzeroes != 0 )
            {
                --number;
                ++ctr;
            }
            ctr /= (trailingzeroes/10);

            product*=ctr;

    }
    return product;
}

int main()
{
       std::cout << getProduct(1231231231231);
}




If you want it to take less time, you'll have to write a more efficient way to make these calculations.
Last edited on
Does it freeze (stop doing anything) or does it just take a long time? 


it freezes, because line 56 should output something, but it freezes, when i comment out line 58, it doesnt get freezes

its like the program is skipping line 56
Not quite. Line 56 should put something in the output buffer, ready to be flushed (and displayed on screen), but there's no guarantee that it will immediately be flushed and displayed on screen. If you want to force it to flush and definitely go to the screen, add an endl or a flush.

It's not freezing. It takes a very long time. Here is a faster implementation of a function that multiplies together all the digits in a number. It uses C++11, but that's not important. It could easily be done with C++98. What's important is that the algorithm is simply doing much less work.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <string>
unsigned long long getProduct(unsigned long number)
{
  std::string asString = std::to_string(number);

  long long total = 1;

  for (auto& individual : asString)
    {
      total = total * (individual - '0');
    }

  return total;
}

int main()
{
  std::cout << getProduct(1231231231231);
}

Last edited on
isee i got it,


the code is pretty short lol wait ill try it

EDIT: oh you are doing string manipulation, i wanted it to be done without string manipulation

theres a lot of problem in projecteuler.net that can be done using string manipulation but didnt do it
Last edited on
You can do it without string manipulation. You can split the number into individual digits in other ways. You chose a massively, massively inefficient algorithm. Part of the point of Project Euler is to force people to actually think about efficiency of algorithms. As you've seen, your algorithm is so slow as to be useless.

If you already did it with string manipulation, and then decided to try again without that, good for you. Have fun with it.

You should also think about how you couldn't tell if it had frozen, or was just taking a long time. Very, very different things.
Last edited on
Topic archived. No new replies allowed.