Problem 14 Modular Calculator - data types

Back to General discussions forum

drewcocker     2015-01-20 06:02:05
User avatar

I'm having trouble calculating the modulus in problem 14. For the last calculation I've got 1.718 x 10^47 % 9833 but I'm getting weird answers. I've verified 1.718 x 10^47 in Excel (though I can't check the last step because Excel truncates large numbers), so I think the issue is with calculating the modulus.

Should this answer involve numbers of this order (10^47)? If so, what is the appropriate data type? (I'm using C#) Double doesn't have enough significant digits, ulong and decimal don't have enough digits.

Rodion (admin)     2015-01-20 08:28:05
User avatar

Hi, thanks for your question!

> Should this answer involve numbers of this order (10^47)?

No, it should not. Though some languages will allow you to use numbers of arbitrary length, it is not the supposed way to solve this.

Main idea is that you can after any intermediate operation take the modulo of result thus reducing the value.

for example:

(10 + 10) * 7 % 11 = 8

you can calculate 10 + 10 and take the modulo % 11 - this will yield 9 - then multiply it by 7 and again take the modulo % 11 - and still you get 8.

drewcocker     2015-01-21 06:09:31
User avatar

Thanks, that makes sense. I hadn't considered the other properties of the modulo operation.

I like the website, by the way. It's a good step between watching programming tutorials and writing your own programs.

woodser     2015-06-04 21:07:55

Faced with the same problem, thanks for the explanation!

hittingthecoder     2015-06-10 17:04:22

But how can i know what is the number after % operator? The number of digits that will be in the input is also not specified. Please give some advice.

OldProgrammer     2015-06-11 08:12:59

hittingthecoder:

The question only asks you to apply the operations sequentially. It also says that % operator is only on the last line.

It does not say anything about when you should actually start the calculation.

Please login and solve 5 problems to be able to post at forum