Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
179 views

Stable/Patient Sort: Bit-Manipulation-To-Solve-Problems-Easily-And-Efficiently

Lagrange's Four-Square Theorem states that any integer can be expressed as the sum of four square numbers. It provides methods to determine if a number can be expressed as 1, 2, 3, or 4 square numbers. The Sum of Two Squares Theorem states that any integer greater than 1 can be expressed as the sum of two square numbers if its prime factors do not include primes congruent to 3 modulo 4 raised to an odd power. It provides an example of how to apply this rule. The document also summarizes formulas for the sum of the first N natural numbers and methods for bit manipulation, digital roots, and definitions of subsequence, substring, and subset.

Uploaded by

mmjon
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
179 views

Stable/Patient Sort: Bit-Manipulation-To-Solve-Problems-Easily-And-Efficiently

Lagrange's Four-Square Theorem states that any integer can be expressed as the sum of four square numbers. It provides methods to determine if a number can be expressed as 1, 2, 3, or 4 square numbers. The Sum of Two Squares Theorem states that any integer greater than 1 can be expressed as the sum of two square numbers if its prime factors do not include primes congruent to 3 modulo 4 raised to an odd power. It provides an example of how to apply this rule. The document also summarizes formulas for the sum of the first N natural numbers and methods for bit manipulation, digital roots, and definitions of subsequence, substring, and subset.

Uploaded by

mmjon
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Lagrange’s Four-Square Theorem

- Any number can be represented as a sum of 4 squares, meaning we can have 1, 2, 3, or


4 squares
- Check if 1 square: if int(sqrt(x)) == x: return True
- Check if 2 squares:
o Iterate over the square roots from i up to sqrt(x), then check if the leftover
value (x – i*i) is a perfect square or not

- Check if 4 squares: If this value is not of the form 4^k(8m+7)


- Check if 3 squares: if it is not any of these, then it is 3 squares
- If we do not know about the 4^k(8m+7) formula, then we need to check all pairs i,j <
sqrt(x) so n=i*i +j*j + k*k with complexity O(n) and same for 4

Sum of two squares theorem


- Any number greater than 1 can be written as a sum of two squares n=a^2 + b^2 for
some integers a,b iff its prime decomposition contains no prime congruent to 3 % 4
raised to an odd power

Ex. 2450 = 2 · 52 · 72


- 7 is congruent to 3 % 4 but is raised to 2 which is even so it does have a sum of two
squares which is 7^2 + 49^2

Sum of N numbers starting from 1


N * (N+1) / 2

Sorting Algorithms

Stable/Patient Sort
 Create new piles when there is larger number than the other piles. This results in
increasing sequences and is good for largest increasing sequence.

Bit Manipulation

https://leetcode.com/problems/sum-of-two-integers/discuss/84278/A-summary%3A-how-to-use-
bit-manipulation-to-solve-problems-easily-and-efficiently

what is topological sort? Kahns algorithm to


Digital Root
- This is if you have a digit (e.g. 38 or 193), and you sum all the digits iteratively until the
value is a single digit (e.g. 3 + 8 = 11, 1 + 9 + 3 = 13). You can do this in O(1) Time and
Space using the digital root formula without any loop or recursion

"""
check parity (odd or even): & 1
get last bit: x & (x-1)
two complement : res = -(1<<32 - res)
- get the value of a negative bit representation
- we convert it to a value 1<<32, then we need to subtract to get the complement

"""

Negative bit value: the complement of the bit except the last 1 stays 1 bit

5:  0000000000000101

-5: 1111111111111011

 8: 0000000000001000

-8: 1111111111111000

 12: 0000000000001100

-12: 1111111111110100

Subsequence vs Substring
Subarray: contiguous sequence in an array
Subsequence: Need not to be contiguous, but maintains order
Subset: keeps relative order but does not need to be contiguous

You might also like