Skip to content
Programmingoneonone
Programmingoneonone
  • Home
  • CS Subjects
    • IoT ? Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone

HackerEarth Numbers of Interest 2 problem solution

YASH PAL, 31 July 2024
In this HackerEarth Number of Interest 2 problem solution After solving Reese’s first problem Harold thought he had proven himself. But Reese wasn’t convinced so he gave Harold another query. He told Harold to find the nth term of the sequence given by the equation.
a[n]=( f[n] + g[n] ) % n
where, f[n] = f[n-1] + x(n) ; where x(n) = smallest prime factor of n.
and g[n] = g[n-1] + y(n) ; where y(n) = sum of all natural numbers p less than n which follow that n % p == 0
Given : x(0) = x(1) = y(0) = y(1) = 0
HackerEarth Numbers of Interest 2 problem solution

HackerEarth Numbers of Interest 2 problem solution.

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
long long f[1000005];
long long ans[1000005];
long long arr[1000005];
void markMultiples(long long a)
{
long long i=2,num;
while((num=i*a)<=1000005)
{
if(arr[num]==0)arr[num]+=a;
++i;
}
}

void SieveOfEratosthenes()
{
for (long long i=2;i<1000005;++i)
{
if (arr[i]==0)
{
arr[i]=(arr[i-1]+i);
markMultiples(i);
}
else
arr[i]+=arr[i-1];
}
}

int main()
{
SieveOfEratosthenes();
memset(f,0,sizeof(f));
memset(ans,0,sizeof(ans));
long long i=0,j=0;
for(i=1;i<=(1000002/2);i++)
{
for(j=i+i;j<1000002;j+=i)
{
f[j]+=i;
}
}
for(i=2;i<1000002;i++)
ans[i]=ans[i-1]+f[i];
int t;
scanf("%d",&t);
while(t--)
{
long long n;scanf("%lld",&n);
printf("%lldn",(ans[n]+arr[n])%n);
}

return 0;
}
coding problems solutions

Post navigation

Previous post
Next post

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes