In number theory, a polite number is a positive integer that can be written as the sum of two or more consecutive positive integers.
The first few polite numbers are
3, 5, 6, 7, 9, 10, 11, 12, 13, 14, 15, 17,...
for instance, 15 = 1 + 2 + 3 + 4 + 5 = 4 + 5 + 6 = 7 + 8. The number of ways that a number can be written as the sum of two or more consecutive numbers is its politeness. Any number that is a power of 2 cannot be written as the sum of two or more consecutive numbers; such a number has a politeness of 0, and is thus impolite.
write a program that calculates all the ways that a number can be written as the sum of two or more consecutive numbers and generate those sets.
Code:
#include<stdio.h>
#include<stdlib.h>
int main()
{
int n,sum,i,j,k,q=0,count=0;
scanf("%d",&n);
for( i=n/2+1; i > 1; i-- )
{
sum=i;
for( j=i-1; j > 0; j-- )
{
sum=sum+j;
if( sum==n )
{
count++;
if( j==1 ) q=1;
for( k=j; k <= i; k++ ) printf("%d ",k);
printf("\n");
break;
}
else if( sum > n ) break;
}
if( q ) break;
}
printf( "total no. of ways: %d",count );
system("pause");
return 0;
}
This comment has been removed by the author.
ReplyDelete#include
ReplyDelete#include
int main()
{
int i,j,n,start=1,curr=0;
printf("enter the number\n");
scanf("%d",&n);
for(i=1;in)
{
curr-=start;
start++;
}
if(curr==n)
{
for(j=start;j<=i;j++)
printf("%d ",j);
printf("\n");
}
}
return;
}