Example:
given n = 81
81 can be represented as 18 * 4 + 9
so n can be represented as 18 4's and one 9. So together 18+1 = 19 number of composite numbers
Output: 19
The least composite number is 4.
so when n is represented as multiple of 4, we get maximum number of composite numbers.
When n is not divisible by 4 we get a remainder 1,2,3
When remainder is 1 then
excess = 1
to make number divisible by 4
we cannot subtract 4+1 = 5 as 5 from n as 5 is not composite number
so we take two 4's = 8 and 8+1 = 9 is next least composite number and hence we subtract 9 from n to make n divisible by 4.
When remainder is 2 then
excess = 2
to make number divisible by 4 we subtract 4+2 = 6 from n.
When remainder is 3 then
excess = 3
we cannot use 4+3= 7 as 7 is not composite number
so we take two 4's = 8 and 8+3 = 11 is not composite number
so we take three 4's= 12 and 12+3 = 15 is next least composite number and hence we subtract 15 from n to make n divisible by 4.
and 15 can be expressed as 9 and 5
#include<stdio.h>
int main()
{
int n,ans= -1,r;
scanf("%d",&n);
if(n >= 4)
{
r=n%4;
if(r == 0)
ans = n/4;
else if (r == 1)
{
if(n >= 9)
ans = (n - 9)/4+1;
}
else if(r == 2)
ans = (n - 6)/4 + 1;
else if(r == 3)
{
if(n >= 15)
ans = (n - 15)/4+2;
}
}
if(ans == -1)
printf("Not Possible");
else
printf("%d",ans);
}
Efficient Solution
If the number is 5, 7, 11 then it is impossible to represent it as sum of composite numbers.
#include<stdio.h>
int main()
{
int n,ans= -1,r;
scanf("%d",&n);
if( n == 5 || n == 7 || n == 11)
ans = -1;
else if(n >= 4)
{
r=n%4;
if(r == 0 || r == 2)
ans = n/4;
else if (r == 1 || r == 3)
ans = n /4-1;
}
if(ans == -1)
printf("Not Possible");
else
printf("%d",ans);
}
given n = 81
81 can be represented as 18 * 4 + 9
so n can be represented as 18 4's and one 9. So together 18+1 = 19 number of composite numbers
Output: 19
The least composite number is 4.
so when n is represented as multiple of 4, we get maximum number of composite numbers.
When n is not divisible by 4 we get a remainder 1,2,3
When remainder is 1 then
excess = 1
to make number divisible by 4
we cannot subtract 4+1 = 5 as 5 from n as 5 is not composite number
so we take two 4's = 8 and 8+1 = 9 is next least composite number and hence we subtract 9 from n to make n divisible by 4.
When remainder is 2 then
excess = 2
to make number divisible by 4 we subtract 4+2 = 6 from n.
When remainder is 3 then
excess = 3
we cannot use 4+3= 7 as 7 is not composite number
so we take two 4's = 8 and 8+3 = 11 is not composite number
so we take three 4's= 12 and 12+3 = 15 is next least composite number and hence we subtract 15 from n to make n divisible by 4.
and 15 can be expressed as 9 and 5
#include<stdio.h>
int main()
{
int n,ans= -1,r;
scanf("%d",&n);
if(n >= 4)
{
r=n%4;
if(r == 0)
ans = n/4;
else if (r == 1)
{
if(n >= 9)
ans = (n - 9)/4+1;
}
else if(r == 2)
ans = (n - 6)/4 + 1;
else if(r == 3)
{
if(n >= 15)
ans = (n - 15)/4+2;
}
}
if(ans == -1)
printf("Not Possible");
else
printf("%d",ans);
}
Efficient Solution
If the number is 5, 7, 11 then it is impossible to represent it as sum of composite numbers.
#include<stdio.h>
int main()
{
int n,ans= -1,r;
scanf("%d",&n);
if( n == 5 || n == 7 || n == 11)
ans = -1;
else if(n >= 4)
{
r=n%4;
if(r == 0 || r == 2)
ans = n/4;
else if (r == 1 || r == 3)
ans = n /4-1;
}
if(ans == -1)
printf("Not Possible");
else
printf("%d",ans);
}
No comments:
Post a Comment