polite numbers

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.

Comments

  1. #include

    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 );
    return 0;
    }

    ReplyDelete
  2. @jainendra your approach is perfectly working but still i would suggest you to read polite numbers.They have some amazing property using that you can find it in more optimized way..

    ReplyDelete
  3. yeah they really hv...thanks for suggesting :)

    ReplyDelete

Post a Comment

Popular posts from this blog