Modulous without division for powers of two - bit-manipulation

with reference to, modulous formula
can someone explain how a mod m = ( (b mod m)(a/b) + (a mod b) ) mod m works in case of for powers of two (2^n).
With base 10 the explanation is clear, but unable to do it for base 2 numbers, e.g :-
9 mod 4 = ( ( 2 mod 4)(9/2) + (9 mod 2) ) mod 4, how can i deduce it to 9 & (4-1)?

Related

Working with large integer data type, possibly larger than 10^18

What data type or what ways can I store large integers possibly greater than 10^18 and How can I efficiently improve my approach to the problem?
I am currently working on a problem that asks to find the sum of all divisors d(k) given that:
N N
S(N) = ∑ ∑ d(j*i)
i=1 j=1
with the largest value of N = 10^9 and largest divisor (10^9 * 10^9). Stored in:
long long int
The program solves and slows down at N = 10^3 and anything higher takes up to much memory and crashes.
I used a for loop for the values of i and j that calculates the values of d(k) > d(i * j) and store it in a vector:
{d(1 * 1), d(1 * 2), ... , d(i>N * j>N)}
Then a separate function that finds all divisors of d(k) then adds them up:
d(1) = 1
d(2) = 1 + 2 = 3
d(3) = 1 + 3 = 4
d(4) = 1 + 2 + 4 = 7
...
d(i>N * j>N)
S(N) = d(1) + d(2) + d(3) + d(4) + ... + d(i>N * j>N)
Any values of N greater than 10^5 gets displayed as S(N) mod 10^9.

C++ last digit of a random sequence of powers

I realise that there are several topics already covering this. But my question is not regarding how to build such an algorithm, rather in finding what mistake I have made in my implementation that's causing a single test out of dozens to fail.
The challenge: supplied with a std::list<int> of random numbers, determine the last digit of x1 ^ (x2 ^ (x3 ^ (... ^ xn))). These numbers are large enough, or the lists long enough, that the result is astronomical and cannot be handled by traditional means.
My solution: I chose to use a modular arithmetic approach. In short, the last digit of these huge powers will be the same as that of a reduced power consisting of the first digit of the base (mod 10), raised to the last two digits of the exponent (mod 100). The units in a sequence of powers repeat in patterns of 4 at most, so we can use mod 4 to reduce the exponent, offset by 4 to avoid remainders of 0. At least, this is my understanding of it so far based on the following resources: brilliant / quora.
#include <list>
#include <cmath>
int last_digit(std::list<int> arr)
{
// Break conditions, extract last digit
if (arr.size() == 1) return arr.back() % 10;
if (arr.size() == 0) return 1;
// Extract the last 2 items in the list
std::list<int> power(std::prev(arr.end(), 2), arr.end());
arr.pop_back(); arr.pop_back();
// Treat them as the base and exponent for this recursion
long base = power.front(), exponent = power.back(), next;
// Calculate the current power
switch (exponent)
{
case 0: next = 1; break;
case 1: next = base % 100; break;
default: next = (long)std::pow(base % 10, exponent % 4 + 4) % 100;
}
if (base != 0 && next == 0) next = 100;
// Add it as the last item in the list
arr.push_back(next);
// Recursively deal with the next 2 items in the list
return last_digit(arr);
}
Random example: 123,232 694,027 140,249 ≡ 8
First recrusion: { 123'232, 694'027, 140'249 }
base: 694,027 mod 10 = 7
exponent: 140,249 mod 4 + 4 = 5
next: 75 = 16,807 mod 100 = 7
Second recursion: { 123'232, 7 }
base: 123,232 mod 10 = 2
exponent: 7 mod 4 + 4 = 7
next: 27 = 128 mod 100 = 28
Third recursion: { 28 }
return: 28 mod 10 = 8
The problem: this works for dozens of test cases (like the one above), but fails for 2 2 101 2 ≡ 6.
By hand:
1012 = 10,201
210,201 mod 4 = 0, + 4 = 4
24 = 16 // 6 -correct
Following the algorithm, however:
First recursion: { 2, 2, 101, 2 }
base: 101 mod 10 = 1
exponent: 2 mod 4 + 4 = 6
next: 16 = 1 mod 100 = 1
Second recursion: { 2, 2, 1 } (we can already see that the result is going to be 4)
exponent = 1, next = 2 mod 100 = 2
Third recursion: { 2, 2 }
base: 2 mod 10 = 2
exponent: 2 mod 4 + 4 = 6
next: 26 = 64 mod 100 = 64
Fourth recursion: { 64 }
return 64 mod 10 = 4 // -wrong
In a way, I see what's going on, but I'm not entirely sure why it's happening for this one specific case, and not for dozens of others. I admit I'm rather pushing the limits of my maths knowledge here, but I get the impression I'm just missing a tiny part of the puzzle.
I reckon this post is long and arduous enough as it is. If anyone has any insights into where I'm going wrong, I'd appreciate some pointers.
There's a lot of problems regarding the modulo of a really big number and a lot of the sol'ns back there was basically based on basic number theory. Fermat's Little Theorem, Chinese Remainder Theorem, and the Euler's Phi Function can all help you solve such problems. I recommend you to read "A Computational Introduction to Number Theory and Algebra" by Victor Shoup. It'll help you a lot to better simplify and approach number theory-related questions better. Here's the link for the book in case you'll browse this answer.

prime numbers algorithm efficiency

I have a question about prime numbers algorithm.
why in the following pseudo code i increases by 6 and not by 2 every iteration?
function is_prime(n)
if n ≤ 1
return false
else if n ≤ 3
return true
else if n mod 2 = 0 or n mod 3 = 0
return false
let i ← 5
while i * i ≤ n
if n mod i = 0 or n mod (i + 2) = 0
return false
i ← i + 6
return true
Thanks!
If it increased by 2 it would be testing almost everything twice, that wouldn't make any sense. So I assume you mean: how can it get away with not testing every odd number?
This is because every prime p greater than 3 is of the form 6n±1. Proof:
Consider the remainder r = p mod 6. Obviously r must be odd. Notice also that r cannot be 3, because then p would be divisible by 3, making it not a prime. This leaves only the possibilities 1 and 5, which correspond p being of the form 6n+1 or the form 6n-1 respectively.
The effect is that it avoid testing multiples of 3. Dividing by a multiple of 3 is redundant, because we already know that n is not a multiple of 3, so it cannot be the multiple of a multiple of 3 either.
The assignment in the loop body is i <- i + 6, not i <- i + 2. In the if statement the expression i + 2 just becomes a new value. There is no assignment operator in that expression.
The algorithm is based on the fact that prime numbers can be predicted using the formula 6k ± 1 and this does not apply on 2 and 3.
For instance
(6 * 1) - 1 = 5
(6 * 2) - 1 = 11
(6 * 3) - 1 = 17
The list goes on and on.

Explain this bit of code to a beginner [duplicate]

This question already has answers here:
What is the result of % in Python?
(20 answers)
Closed 6 years ago.
for x in xrange(12):
if x % 2 == 1:
continue
print x
i know what it does, but the language doesn't make sense to me. In particular the second line is where i am lost.
if x % 2 == 1 means "if x modulo 2 equals 1".
Modulo (or mod) is the remainder after division. So, for example:
3 mod 2 = 1
12 mod 5 = 2
15 mod 6 = 3
For x mod 2, you're there's a remainder if and only iff x is odd. (Because all even numbers are divisible by two with 0 remainder.) Likewise, odd numbers will always have a remainder of 1.
So x % 2 == 1 returns true if x is odd.

Recursive function (help me understand it by pen and paper)

First of all I have to say that I can use recursive functions on easy examples like Fibonacci, but I can't understand how to dry run (solve with pen and paper) this recursion :
#include<iostream>
using namespace std;
int max(int a, int b)
{
if(a>b)return a;
return b;
}
int f(int a, int b)
{
if(a==0)return b;
return max( f(a-1,2*b), f(a-1,2*b+1) );
}
int main()
{
cout<<f(8,0);
}
How do I do this with pen and paper, with say, a = 5 and b = 6?
We have always a depth of a (8)
Each invocations calls itself 2 times, once 2b and once 2b+1 is passed
The greater result of both calls is returned
As 2b + 1 > 2b only the right site of the max call is meaningful (2b + 1)
Now lets do the first iterations mathematically:
2 * b + 1 = 2^1 * b + 2^0
2 * (2^1 * b + 2^0) + 1 = 2^2 * b + 2^1 + 2^0
2 * (2^2 * b + 2^1 + 2^0) + 1 = 2^3 * b + 2^2 + 2^1 + 2^0
2 * (2^3 * b + 2^2 + 2^1 + 2^0) + 1 = 2^4 * b + 2^3 + 2^2 + 2^1 + 2^0
As you can see there is a system behind it. Because b = 0 for the first iteration, we can ignore the left side. The final value is thus:
2^0 + 2^1 + 2^2 + 2^3 + 2^4 + 2^5 + 2^6 + 2^7
=
1 + 2 + 4 + 8 + 16 + 32 + 64 + 128
=
255
If we run the programm we get the exact same value
Just to give some information there are algorithms that use a little more complex parameters, one basic example would be mergesort
Merging is simple:
Take two elements one from each array A and B.
Compare them and place smaller of two (say from A) in sorted list.
Take next element from A and compare with element in hand (from B).
Repeat until one of the array is exhausted.
Now place all remaining elements of non-empty array one by one.
Maybe you can find this doc useful
Or maybe this one
Assuming you want to analyzse the funciton on paper, I'll paste the result for f(1, 2)
f(2, 1) =
max( f(1, 2), f(1, 3) ) =
max ( max(f(0, 4), f(0, 5) , max(f(0, 6), f(0, 7) ) =
max ( max(4, 5) , max(6, 7) ) =
max (5, 7) =
7
Is up to you to follow the computations
Note: I'm also assuming you didn't miss a parenthesis here: 2*b+1