Question: Swiss Re, Recently Asked Online Assessments in 29 Aug, 2025 | Password Generator (Sum of Divisors)
0
Entering edit mode

Question: File Password Generator (Sum of Divisors)

Problem Statement:

John, an information security consultant, recently joined an organization. He noticed that employees were sending files as email attachments without any password protection. He advised them to enhance security by using passwords for their files and emphasized not to share the password in the same email. His recommendation was to use a number as the password for the file.

To facilitate this process, John suggested including a number N in the email body, which could be used to generate the password. Clients wanting to open the file would need to identify all the numbers that can evenly divide N. The sum of these divisors will serve as the password to access the file.

As a software developer in the organization, you have been tasked with creating a webpage where clients can input N and receive the corresponding password to open the file.

Read the input from STDIN and print the output to STDOUT. Do not write arbitrary strings while reading the input or while printing, as these contribute to the standard output.

Constraints:

  • 0 < N < 10000

Input Format:

  • The input contains an integer, N, the number specified in the email.

Output Format:

  • The output contains the password of the file.
ADD COMMENTlink 18 days ago Rohit • 40
0
Entering edit mode

Problem Solution

Question: File Password Generator (Sum of Divisors)

Topics Involved / Prerequisites

  • Number Theory
  • Factors and Divisors
  • Modulo Arithmetic

Overview

The objective is to calculate the sum of all divisors of a given integer N. A divisor is any integer d that divides N without leaving a remainder (N \pmod d = 0). For example, if N=6, the divisors are 1, 2, 3, and 6, so the password is 1 + 2 + 3 + 6 = 12.

Approach

  1. Read Input: Since N is a single integer, read it from standard input (cin).
  2. Iterative Search: Since the constraint on N is relatively small (N < 10,000), a linear search from 1 to N is highly efficient.
  3. Condition Check: For every integer i from 1 to N:
    • Check if N is divisible by i using the modulo operator (N % i == 0).
    • If the condition is true, add i to a running total_sum.
  4. Output Result: Print the total_sum to standard output. Ensure no additional text like "Password is:" is printed, as per the requirements.

Code Implementation (C++)

#include <iostream>

using namespace std;

int main() {
    int n;
    // Read the input number N
    if (!(cin >> n)) return 0;

    int sum_of_divisors = 0;

    // Iterate from 1 to N to find all numbers that evenly divide N
    for (int i = 1; i <= n; i++) {
        if (n % i == 0) {
            sum_of_divisors += i;
        }
    }

    // Print the final result (the password)
    cout << sum_of_divisors << endl;

    return 0;
}

Time and Space Complexity

  • Time Complexity: O(N) — We iterate through all numbers from 1 to N once. Given N < 10,000, this results in at most 10,000 operations, which executes instantly.
  • Space Complexity: O(1) — No additional data structures are used regardless of the size of N.


 

ADD COMMENTlink 4 days ago admin 1.9k

Login before adding your answer.

Similar Posts
Loading Similar Posts