- #1

- 7

- 0

You are using an out of date browser. It may not display this or other websites correctly.

You should upgrade or use an alternative browser.

You should upgrade or use an alternative browser.

- Thread starter paut
- Start date

In summary: Thank you very much! Now I have to think about the formula for n+2^(n-2) and I will be done!In summary, the conversation discusses finding a way to disassemble the operation ((n+2^(n-2))^2 mod 1000000007) without counting 2^n-2. The suggested methods include using modular arithmetic algorithms and a function called "mod_pow" to calculate the result. The conversation also mentions the use of strings and the need to display huge numbers. Finally, the conversation concludes with a comparison of the output of the "mod_pow" function with other implementations and the agreement of the results.

- #1

- 7

- 0

Physics news on Phys.org

- #2

Homework Helper

MHB

- 16,336

- 258

Well, if n<15, then the result is simply the same, since it would be less than 1000000007.

For larger numbers you can reduce the power by using that (2^1000000006 mod 1000000007)=1.

- #3

- 697

- 2

r1 = modular_exponentiation of 2 to the n-2, performed modulo 1000000007

r2 = r1 + n, modulo 1000000007

r3 = modular_exponentiation of r2 to the 2, performed modulo 1000000007

and your result is r3.

Google for "modular exponentiation algorithm"; a method simple to implement is the "russian peasant algorithm" (google for that).

In order to implement this, you'll need another method for modular multiplication, because calculating (a * b) % 1000000007 will likely give you an overflow in the a*b product, before you get to reduce it modulo 1000000007. But that is not hard to devise (shifting and adding), or google again for "peasant multiplication" and adapt it with a modulo 1000000007 at each step. (You'll see what I mean when you get hands-on into it.)

There are of course more complicated algorithms (of which I can't speak because I don't really know), but the above are manageable simple.

- #4

- 7

- 0

For:

y <- n + mod_pow (2, n-2, 1000000007).

result <- mod_pow (y, 2, 1000000007).

I get an incorrect result, so I had misunderstood something. Very please to explain.

- #5

- 697

- 2

Can you show the code for mod_pow? It may have an error, or maybe it is designed to work with a parameter value which is already under 1000000007 (and "y", on the second call, is not).

Last edited:

- #6

Homework Helper

MHB

- 16,336

- 258

paut said:

For:

y <- n + mod_pow (2, n-2, 1000000007).

result <- mod_pow (y, 2, 1000000007).

I get an incorrect result, so I had misunderstood something. Very please to explain.

Your formulas look right.

If it does not work, I expect an overflow, which would typically occur on a 32 bits platform.

What is your platform?

What is your computer language?

What is the type of the parameters to mod_pow?

What is the type of the variables you use to calculate?

- #7

- 7

- 0

My work for the sequences of length 'n' is finding a number of variations elements of two-piece set, in this way that one of this two elements (doesn't matter which) will never occur next to another.

If set is composed just of '0' and '1' then possible combinations looks like this (I assume that element that will never appear next to another is ‘1’):

For n=2: 00 01 10 (result – 3)

For n=3: 000 001 010 100 101 (result - 5)

For n=4: 0000 0001 0010 0100 1000 1010 0101 1001 (result – 8)

I have to show result as: result^2 mod 1000000007. Maximum of 'n' is 2^31 -1 that is 2147483647. Maximum of calculating time is 1s.

One of my problems is to find a formula that gives me a number of combinations. I noticed some dependency between another results and I created a formula n+2^(n-2) for n>1, unfortunately I'm not 100% sure that this formula is correct ( I checked that for low 'n' this formula is correct).

My another problem is displaying a huge numbers. If n=2000000000 then calculating 2^(n-2) become impossible because of the size of result. However doing the calculations on string takes a lot of time.

Because of this it became obvious for me that I need to use fact that the final result have to be modulo 1000000007. At the beginning I turned in this forum with this problem.

On the web I found two implementations of modular exponentiation :

1: „unsigned long long int mod_pow(unsigned long long int num, unsigned long long int pow, unsigned mod)

{

unsigned long long int test,n=num;

for(test = 1; pow; pow >>= 1)

{

if (pow & 1)

test = ((test % mod) * (n % mod)) % mod;

n = ((n % mod) * (n % mod)) % mod;

}

return test;

}”

2: „unsigned long long int power_modulo_fast(unsigned long long int a, unsigned long long int b)

{

unsigned long long int x=a%1000000007,result=1,i;

for (i=1; i<=b; i<<=1)

{

x %= 1000000007;

if ((b&i) != 0)

{

result *= x;

result %= 1000000007;

}

x *= x;

}

return result;

}”The final code is tested on the internet checker not on my computer, so parameters of my computer really doesn't matter.

I wrote a program for test which operating on strings so exponentiation 2^1000 isn't problem, but when I'm testing this program on my computer I get other result than those which I recived after using the above functions. Unfortunately I can't take this program as my official solution of this problem, because calculations takes a lot of time for higher data like n=1000000.

- #8

- 697

- 2

From what I can see at first glance, the code for these two functions looks fine; but you may want to check that, in your machine and C compiler, "sizeof (long long int)" returns 8 (8 bytes = 64 bits).

In case this helps, I compared the output of your two functions with my own implementation, and the three of them agree for a few test values of n:

For n=2,3,4,10,20,456,456000 and 4560000, I also checked with the Linux multiple-precision calculator "bc", and the result agrees with the above. (For 45600000, 456000000 and 500000005 I didn't check, since "bc" would take too long.) For the last test value, 1000000008, the result 4 is theoretically correct, because of the reason "I like Serena" explained in post#2.

You may want to check these values in your "string" program, too.

P.S.: You may want to google for "arbitrary precision calculator"; a few tools are free to download, and there may even be some to use online on your browser. These could be helpful in checking your results.

P.P.S: Confirmed all values above using the GP/Pari calculator.

In case this helps, I compared the output of your two functions with my own implementation, and the three of them agree for a few test values of n:

Code:

```
Evaluations of (n + 2^(n-2))^2 mod 1000000007, for n = ...
2: 9 9 9
3: 25 25 25
4: 64 64 64
10: 70756 70756 70756
20: 729962420 729962420 729962420
456: 781936176 781936176 781936176
456000: 832296930 832296930 832296930
4560000: 786542175 786542175 786542175
45600000: 98463981 98463981 98463981
456000000: 494805060 494805060 494805060
500000005: 250000008 250000008 250000008
1000000008: 4 4 4
```

For n=2,3,4,10,20,456,456000 and 4560000, I also checked with the Linux multiple-precision calculator "bc", and the result agrees with the above. (For 45600000, 456000000 and 500000005 I didn't check, since "bc" would take too long.) For the last test value, 1000000008, the result 4 is theoretically correct, because of the reason "I like Serena" explained in post#2.

You may want to check these values in your "string" program, too.

P.S.: You may want to google for "arbitrary precision calculator"; a few tools are free to download, and there may even be some to use online on your browser. These could be helpful in checking your results.

P.P.S: Confirmed all values above using the GP/Pari calculator.

Last edited:

- #9

- 7

- 0

Here are the calculations that should be correct: http://img695.imageshack.us/img695/3950/calcw.png [Broken]

My program returns the same result (the first based on a string and second without them). I also have exactly the same results as you. The problem is probably in the wrong set formula :/ I see no other option :/

Thank you very much for your help which was given to me here ;)

My program returns the same result (the first based on a string and second without them). I also have exactly the same results as you. The problem is probably in the wrong set formula :/ I see no other option :/

Thank you very much for your help which was given to me here ;)

Last edited by a moderator:

- #10

Homework Helper

MHB

- 16,336

- 258

paut said:

My program returns the same result (the first based on a string and second without them). I also have exactly the same results as you. The problem is probably in the wrong set formula :/ I see no other option :/

Thank you very much for your help which was given to me here ;)

Hmm, I see another option, which is that your results are right.

What is your reason to think that they are wrong?

Last edited by a moderator:

- #11

- 7

- 0

- #12

Homework Helper

MHB

- 16,336

- 258

paut said:

So let's assume for now that your mod_pow method of evaluation is right (which I think it is).

Is it possible something else is wrong?

- #13

- 7

- 0

Combinations which I could find (33):

0000000

0000001 0000010 0000100 0001000 0010000 0100000 1000000

1000001 1000010 1000100 1001000 1010000 0100010 0100001 0101000 0010010 0010100 0010001 0001001 0001010 0000101

1010001 1010010 1010100 0101010 0100101 0101001 1000101 1001010 1001001 0010101

1010101

- #14

Homework Helper

MHB

- 16,336

- 258

You might search for bernoulli trials and k successes in a row, for more specific information.

(You want all sequences that have at least 2 successes in a row. Or rather, the inverse of that.)

- #15

- 7

- 0

Ok, I know. This is the Fibonacci series (2,3,5,8,13,21,34 ...).

How can I calculate: fib(n)^2 mod 1000000007, even for very large numbers?

Is it correct? https://ideone.com/cuGkK

How can I calculate: fib(n)^2 mod 1000000007, even for very large numbers?

Is it correct? https://ideone.com/cuGkK

Last edited:

- #16

Homework Helper

MHB

- 16,336

- 258

And yes, your program looks correct at first sight.

I take it you verified it with a couple of test values?

The modulo operation, denoted by the % symbol, calculates the remainder after dividing one number by another. For example, 10 % 3 would result in a remainder of 1, as 10 divided by 3 is 3 with a remainder of 1. This operation is commonly used in programming to determine whether a number is even or odd, or to cycle through a set of values.

The modulo operation can be used to reduce operations by limiting the range of values that need to be calculated. For example, instead of calculating the sum of all numbers from 1 to 100, we can use the modulo operation to determine the sum of all odd numbers from 1 to 100, which would require fewer operations.

Reducing operations using the modulo operation can save time and resources, as it eliminates the need for unnecessary calculations. This can be especially useful when dealing with large numbers or complex algorithms. It can also improve the efficiency and speed of a program.

Yes, there are some limitations to using the modulo operation. One limitation is that it can only be used with integers, not decimals or fractions. Additionally, it may not be suitable for all types of calculations and may not always result in a significant reduction in operations.

Most programming languages have a modulo operator, but the syntax may vary. Some languages use the % symbol, while others use the keyword "mod". It is important to check the documentation of the specific language you are using to ensure the correct usage of the modulo operation.

Share:

- Replies
- 6

- Views
- 750

- Replies
- 1

- Views
- 514

- Replies
- 11

- Views
- 1K

- Replies
- 19

- Views
- 1K

- Replies
- 9

- Views
- 891

- Replies
- 4

- Views
- 935

- Replies
- 10

- Views
- 1K

- Replies
- 3

- Views
- 1K

- Replies
- 3

- Views
- 768

- Replies
- 2

- Views
- 148