Skip to content

Conversation

@Elghrabawy
Copy link

No description provided.

@spike1236
Copy link
Member

Thanks for the PR! The algorithm seems to be correct, but there are a couple of integer overflow issues that need to be fixed first before merging:

  1. L, R, and all loop variables dealing with ranges up to 1e14 must be long long, not int.
  2. Multiplications like i * prime[j] and p * p need to be done in 64-bit (1ll * i * p) to avoid overflow in the sieve and segmented loop.
  3. The arrays phi[] and rem[] must use long long instead of int, since φ(n) can be > 2³¹ for n up to 1e14.

Updated the segmented_phi function to handle long long types for larger ranges and adjusted related variables accordingly.
@spike1236
Copy link
Member

Clarify the φ(n) computation in the text description - right now it's underspecified. Move the relevant notes from comments into the main text so they're not overlooked. Also include the linear sieve preprocessing complexity.

**Complexity:**
- Preprocessing: $O(\sqrt{R})$ time and space for the linear sieve
- Query: $O((R - L + 1) \log \log R)$ for computing φ over the range
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not query step. Combine all of the text into single paragraph with complexity and usage included. Also, it's better to move the whole section upper, placing it under the "Finding the totient from 1 to  $n$  using the divisor sum property" section. As far as I understand, your idea does not use Gauss' divisor sum property.

Copy link
Member

@spike1236 spike1236 Dec 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once these issues are fixed, the section should be ready to be merged. Also, if possible, it would be great to add tests for the code in the test/ folder.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants