• Loading...

Problem 3: Non-Primes

Points:

Small Case: 8 Answer: 11007

Large Case: 16 Answer: 521907490474536813

Let n be a non-prime number and x be its smallest divisor(not 1) and f(n) be a function such that f(n) = x*n.

The problem is to find the function s(n) which is defined as the sum of all f(i) for all possible i less than and equal to n.

e.g for s(10)=83

Small Case: n=100

Large Case: n=10^8

Solution:

For all the multiples(j) of numbers(i)(2 to sqrt(n)) which haven't occured previosuly, i*j is added to the answer.


#include<iostream>

#include<cmath>

using namespace std;

bool ar[100000001]={0};

int main()

{

unsigned long long int i,j;

unsigned long long int s=0;

unsigned long long int n;

cin>>n;

for(i=2;i<=sqrt(n);i++)

{

	if(ar[i]==0)

	{

		for(j=i*i;j<=n;j+=i)

		{

			if(ar[j]==0)

				s+=(i*j);

			ar[j]=1;

		}

	}

	

}

cout<<s;



return 0;

}

Submit your solution

You need to be logged in to submit a solution.

discussions


jaydev13example
Q:how s(10)=83
A:for n=4 : f(4)=4*2=8
6 : f(6)=6*2=12
8 : f(8)=8*2=16
9 : f(9)=9*3=27
10: f(10)=10*2=20

adding all we get 83

post a question

Loading...

Problem 1
Problem 2
Problem 3
Problem 4
Problem 5 (bonus) Expired
Problem 1
Problem 2
Problem 3
Problem 4 (bonus) Expired
Top Scorers
rng_58 235
Anton_Lunyov 225
tourist 190
Silence 180
cedricl2 175
cgy4ever 175
DrKorbin 165
Koen 155
bbbasic 150
lunae_2 150
Submissions
Problem 1
10 pt
not submitted
98
submissions

19
correct (19 %)



25 pt
not submitted
2
submissions

1
correct (50 %)


Problem 2
5 pt
not submitted
95
submissions

38
correct (40 %)



10 pt
not submitted
19
submissions

15
correct (78 %)


Problem 3
5 pt
not submitted
153
submissions

117
correct (76 %)



15 pt
not submitted
44
submissions

25
correct (56 %)


Problem 4
10 pt
not submitted
152
submissions

54
correct (35 %)



15 pt
not submitted
23
submissions

10
correct (43 %)


Problem 5
10 pt
not submitted
36
submissions

28
correct (77 %)



20 pt
not submitted
6
submissions

2
correct (33 %)


Problem 6
5 pt
not submitted
41
submissions

29
correct (70 %)



10 pt
not submitted
15
submissions

14
correct (93 %)


Problem 7
5 pt
not submitted
38
submissions

17
correct (44 %)



10 pt
not submitted
16
submissions

15
correct (93 %)


Problem 8
5 pt
not submitted
127
submissions

104
correct (81 %)



15 pt
not submitted
47
submissions

34
correct (72 %)


Problem 9
5 pt
not submitted
86
submissions

35
correct (40 %)



15 pt
not submitted
16
submissions

15
correct (93 %)


Problem 10
5 pt
not submitted
45
submissions

23
correct (51 %)



15 pt
not submitted
15
submissions

14
correct (93 %)


Problem 11
5 pt
not submitted
81
submissions

27
correct (33 %)



15 pt
not submitted
8
submissions

3
correct (37 %)


Problem 12
5 pt
not submitted
104
submissions

88
correct (84 %)



10 pt
not submitted
31
submissions

21
correct (67 %)