-2

Given an integer, we need to find the super digit of the integer.

We define super digit of an integer x using the following rules:

If x has only 1 digit, then its super digit is x Otherwise, the super digit of is equal to the super digit of the digit-sum of x. Here, digit-sum of a number is defined as the sum of its digits.

Example:

super_digit(9875) = super_digit(9+8+7+5) 

= super_digit(29) 

= super_digit(2+9)

= super_digit(11)

= super_digit(1+1)

= super_digit(2)

= 2

Task: You are given two numbers n and k. You have to calculate the super digit of p.p is created when number n is concatenated k times.

My Code to solve this problem is shown below.

#include <bits/stdc++.h>
#include <string>
#include <iostream>
#include <math.h>

using namespace std;

int superDigit(string n, int k)
{
    int res=0;
    for (int x = 0; x < n.length(); x++)
    {
        res += n[x] - '0';
    }
    res = k * res;
    if (res < 10)
        return res;
    else
        return superDigit(to_string(res),1);
}

int main() {
    string n;
    int k;
    cin >> n;
    cin >> k;
    cout << superDigit(n, k)<< endl;
    return 0;
}

The code seems to work normally for all small numbers, but when n is 1e100000-1 and k is 100000, the program returns the following error:

terminate called after throwing an instance of 'std::bad_alloc'

what(): std::bad_alloc

I think it's a memory leak, but how do I fix this. Where is the leak happening?

Follj
  • 1
  • 3
  • 2
    It's not because of memory leak. It was unable to allocate the specified amount of memory. More info can be found here: http://en.cppreference.com/w/cpp/memory/new/bad_alloc – Asesh Dec 27 '17 at 05:25
  • 1
    Use a debugger. It will stop at the point that throws the exception. Very useful. – nwp Dec 27 '17 at 05:26
  • Good to know that, but how can I solve this. – Follj Dec 27 '17 at 05:32
  • The first thing would probably be eliminating recursion. – user7860670 Dec 27 '17 at 05:42
  • Unrelated: It appears you do not know what `#include ` does, so I recommend giving [Why should I not `#include `?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) a read before it turns into a bad habit. – user4581301 Dec 27 '17 at 05:42
  • `1e100000-1` is not a valid specification of an integer. – Cheers and hth. - Alf Dec 27 '17 at 05:43
  • x+x+x+x+x can be reduced to just 5*x. – Cheers and hth. - Alf Dec 27 '17 at 05:45
  • **Unable to reproduce**. Are you sure this is the code you used? – Cheers and hth. - Alf Dec 27 '17 at 05:48
  • @Cheersandhth.-Alf `1e100000-1` is just for understanding. While compiling, I have to write 100000 consecutive 9's. Obviously I can't write that long number in a console, so I put the input directly in the code and comment out `cin >> n` – Follj Dec 27 '17 at 05:48
  • @Follij: 10^5 * 10^5 = 10^10 = 100 billion characters. One billion is about 1 GByte. How many GBytes of main memory does your PC have? Have you considered replacing string concatenation with, like, simple multiplication? – Cheers and hth. - Alf Dec 27 '17 at 05:50
  • Ow. That's a pretty big string and a whole lot of iterations to process it. Running out of memory is a distinct possibility even with controlled recursion. – user4581301 Dec 27 '17 at 05:51
  • @Cheersandhth.-Alf Ok, let me try to fix it. – Follj Dec 27 '17 at 06:04
  • Do you mean that you actually typed `1e100000-1` at the input prompt? – M.M Dec 27 '17 at 06:09
  • @M.M `While compiling, I have to write 100000 consecutive 9's. Obviously I can't write that long number in a console, so I put the input directly in the code and comment out cin >> n` so no. – Follj Dec 27 '17 at 06:11
  • **−1** The presented code is no longer the one producing the issue asked about. – Cheers and hth. - Alf Dec 27 '17 at 06:41
  • @Follj in that case you should post the exact code causing the problem output – M.M Dec 27 '17 at 06:49

1 Answers1

-1

You are taking n as 1e10000-1. That is about 1e10000 characters long. Assuming each character is about 1 byte size. Then,

1e10000 bytes = 1e9997 kilobytes = 1e9994 MB = 1e9994 GB = 1e9991 TB As you can see it is insanely large.

  • Re "That is about 1e10000 characters long." Consider how large the source code would have to be for that specification, assuming that the OP put it directly there, as indicated. Is there anything about that that strikes you as, well, noteworthy? – Cheers and hth. - Alf Dec 27 '17 at 06:04
  • I fixed the code (see above), but I get the wrong result `-194313216`. – Follj Dec 27 '17 at 06:16
  • It's `1e100000`, not `1e10000` – Follj Dec 27 '17 at 06:17
  • @Follj: In future, please don't fundamentally change the question when an answer has been posted. Happily this answer was wrong. But if it was a good answer you would just have invalidated it, negating that person's work. – Cheers and hth. - Alf Dec 27 '17 at 06:18
  • @Cheersandhth.-Alf I'll keep that in mind. I am sorry, I am new here. – Follj Dec 27 '17 at 06:20
  • Using Akash Kumar, I finally solved the problem, but then I have a problem with `int` so I replaced every int with `uint64_t`. – Follj Dec 27 '17 at 06:38
  • **−1** To counter the OP's selection as solution, for this nonsense (wrong) answer. – Cheers and hth. - Alf Dec 27 '17 at 06:39