Thursday, August 9, 2012

Miscellaneous – V – Pseudo Perfect No


We already know that a perfect number is a positive integer that is equal to the sum of its proper positive divisors, that is, the sum of its positive divisors excluding the itself. A PseudoPerfect Number A pseudoperfect number, sometimes also called a semiperfect number  Where sum of all or some of its factor equal to the number.

Example : 20 = the factors are 1 2 4 5 10 and if we add 1, 4, 5, 10 leaving 2 we get the number itself, so 20 is a PseudoPerfect number. Other no such 36, 144 etc.




//Pseudo Perfect No

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>

           int ctr;
  void store(int a[],int n)
      {
           int i,j=0;
           printf("\n\tAll Factors of %d is ",n);
           for(i=1;i<=n/2;i++)
              {
                   if(n%i==0)
                        {
                            a[j++]=i;
                            printf(" %d ",i);
                        }
              }

      }
  int sum(int a[],int i,int n,int c,int t)
      {
           int s=a[i],j,k=0,*b;
           b=(int *)malloc(c*sizeof(int));
           b[0]=s;
           k++;
           for(j=t;j>=0;j--)
              {
                   s=s+a[j];
                   b[k++]=a[j];
                   if(s>n)
                        {
                            s=s-a[j];
                            k--;
                        }
                    if(s==n)
                             break;
              }
           if(s==n)
              {
                   ctr++;
                   printf("\n");
                   printf("\n\t%d is a Sudo Perfect and combinations are \n\t\t",n);
                   for(i=k-1;i>=0;i--)
                        {
                            printf("  %d  ",b[i]);
                        }
              }

           return ctr;
      }

  void main()
      {
           int *a,n,j,i,c=0,s=0;
           clrscr();
           printf("\n\n\tEnter a No : ");
           scanf("%d",&n);
           clrscr();
           for(i=1;i<=n/2;i++)
              {
                   if(n%i==0)
                        {
                            s=s+i;
                            c++;
                        }
              }
           if(s==n)
              {
                   printf("\n\t %d is a Perfect no ",n);
                   getch();
                   exit(0);
              }
           a=(int *)malloc(c*sizeof(int));
           store(a,n);
           for(i=c-1;i>=c/2;i--)
              {
                   for(j=i-1;j>=c/2;j--)
                        {
                            s=sum(a,i,n,c,j);
                        }
              }
           if(ctr==0)
              {
                   printf("\n\n\t%d is not Perfect No. or Pseudo Perfect No.",n);
              }
           getch();
      }




No comments: