Program to find out the scalar products of vectors generated from an infinite sequence in Python

We need to find scalar products of vectors generated from an infinite sequence. Given three integers c, m, and n, we generate a sequence where the first value is 0, second is c, and subsequent values follow ki = (ki-2 + ki-1) mod m. From this sequence, we create n vectors using consecutive pairs and calculate scalar products between all vector pairs.

Problem Breakdown

Let's understand with example: c=5, m=6, n=4 ?

  • Generate sequence: [0, 5, 5, 4, 3, 1, 4, 5, 3, 2]
  • Create vectors from consecutive pairs starting from index 2: (5,4), (3,1), (4,5), (3,2)
  • Calculate scalar products between all vector pairs
  • Count unique residues modulo m

Step-by-Step Solution

Generating the Sequence

def generate_sequence(c, m, n):
    """Generate the infinite sequence up to 2n+2 elements"""
    sequence = [0] * (2 * n + 2)
    sequence[0] = 0
    sequence[1] = c
    
    for i in range(2, 2 * n + 2):
        sequence[i] = (sequence[i - 1] + sequence[i - 2]) % m
    
    return sequence

# Example
c, m, n = 5, 6, 4
sequence = generate_sequence(c, m, n)
print("Sequence:", sequence)
Sequence: [0, 5, 5, 4, 3, 1, 4, 5, 3, 2]

Creating Vectors and Computing Scalar Products

def solve(c, m, n):
    if n == 1:
        return 0
    
    # Generate sequence
    temp_arr = [0] * (2 * n + 2)
    temp_arr[0] = 0
    temp_arr[1] = c
    
    for i in range(2, 2 * n + 2):
        temp_arr[i] = (temp_arr[i - 1] + temp_arr[i - 2]) % m
    
    # Create vectors and compute scalar products
    scalar_products = []
    
    # Calculate scalar products between all vector pairs
    for i in range(2, 2 * n - 2, 2):
        # Scalar product with next vector
        temp = (temp_arr[i] * temp_arr[i + 2] + temp_arr[i + 1] * temp_arr[i + 3]) % m
        scalar_products.append(temp)
        
        # Scalar product with vector after next
        temp = (temp_arr[i] * temp_arr[i + 4] + temp_arr[i + 1] * temp_arr[i + 5]) % m
        scalar_products.append(temp)
    
    # Handle the last vector pair
    temp = (temp_arr[2 * n - 2] * temp_arr[2 * n] + temp_arr[2 * n - 1] * temp_arr[2 * n + 1]) % m
    scalar_products.append(temp)
    
    # Count unique residues
    unique_residues = set(scalar_products)
    return len(unique_residues)

# Test the function
result = solve(5, 6, 4)
print(f"Number of unique residues: {result}")
Number of unique residues: 3

Complete Example with Vector Visualization

def solve_with_details(c, m, n):
    if n == 1:
        return 0, [], []
    
    # Generate sequence
    sequence = [0] * (2 * n + 2)
    sequence[0] = 0
    sequence[1] = c
    
    for i in range(2, 2 * n + 2):
        sequence[i] = (sequence[i - 1] + sequence[i - 2]) % m
    
    print(f"Generated sequence: {sequence}")
    
    # Extract vectors
    vectors = []
    for i in range(2, 2 * n, 2):
        vectors.append((sequence[i], sequence[i + 1]))
    
    print(f"Vectors: {vectors}")
    
    # Calculate scalar products
    scalar_products = []
    for i in range(len(vectors)):
        for j in range(i + 1, len(vectors)):
            product = (vectors[i][0] * vectors[j][0] + vectors[i][1] * vectors[j][1]) % m
            scalar_products.append(product)
            print(f"Vector {vectors[i]} · Vector {vectors[j]} = {product}")
    
    unique_residues = set(scalar_products)
    print(f"Scalar products: {scalar_products}")
    print(f"Unique residues: {sorted(unique_residues)}")
    
    return len(unique_residues), vectors, scalar_products

# Test with example
result, vectors, products = solve_with_details(5, 6, 4)
print(f"\nFinal answer: {result}")
Generated sequence: [0, 5, 5, 4, 3, 1, 4, 5, 3, 2]
Vectors: [(5, 4), (3, 1), (4, 5), (3, 2)]
Vector (5, 4) · Vector (3, 1) = 1
Vector (5, 4) · Vector (4, 5) = 4
Vector (5, 4) · Vector (3, 2) = 5
Vector (3, 1) · Vector (4, 5) = 5
Vector (3, 1) · Vector (3, 2) = 5
Vector (4, 5) · Vector (3, 2) = 4
Scalar products: [1, 4, 5, 5, 5, 4]
Unique residues: [1, 4, 5]

Final answer: 3

Key Points

  • Sequence Generation: Uses Fibonacci-like recurrence with modular arithmetic
  • Vector Formation: Consecutive pairs from the sequence starting at index 2
  • Scalar Product: Standard dot product formula: v1·v2 = x1*x2 + y1*y2
  • Edge Case: Returns 0 when n=1 (only one vector, no pairs possible)

Conclusion

This algorithm generates a sequence using modular arithmetic, creates vectors from consecutive pairs, and counts unique scalar product residues. The time complexity is O(n²) for computing all vector pair products, making it efficient for moderate values of n.

Updated on: 2026-03-26T17:52:10+05:30

241 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements