CrazyEngineers Forum
Navigation
Go Back   CrazyEngineers Forum > CE : Technical Discussions > Computer Science & IT Engineering


Advertisements
Reply
 
LinkBack (2) Thread Tools Display Modes

  2 links from elsewhere to this Post. Click to view. #1 (permalink)
Old 9th October 2008, 10:50 AM
Good Administrator
 
The_Big_K's Avatar
 
Join Date: 26th November 2005
I'm a Crazy Electrical Engineer
Location: Terra-Firma
Posts: 8,554
Send a message via Yahoo to The_Big_K
Wink Write a program to display all perfect numbers between 1 - 100

Here's another programming question I've asked in interviews to CS/IT Toppers in interviews when they tell me they are interested in Maths & Programming :


Quote:
Write a program to display all perfect numbers between 1 - 100
Quote:
Computer Language Choice: Whatever, doesn't matter. I'm happy even if you can come up with an algorithm.
PS: If you are interested in mathematics and don't know what Perfect Numbers are, you don't impress me.
The_Big_K is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote


  #2 (permalink)
Old 9th October 2008, 04:07 PM
CE - Newbie
 
c.deepak257's Avatar
 
Join Date: 3rd October 2008
I'm a Crazy computer Engineer
Location: Chennai
Posts: 5
Default Re: Write a program to display all perfect numbers between 1 - 100

2^(n-1)(2^n -1)
this means (2 raise to the power of (n-1)) into (2 raise to the power n)-1

this is the euclid equation for calculating the perfect numbers. The first value of the n is 2 because n-1 should be greater than 1 and n is a prime number i.e. 2,3,5,7 and so on.
use this formula to calculate the perfect numbers between 1 and any number.try yourself. This is the hint.

(P.S.= ASSUMED THAT U KNOW WHAT IS PERFECT NUMBER)

Last edited by c.deepak257; 9th October 2008 at 04:10 PM.
c.deepak257 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)
Old 9th October 2008, 04:18 PM
Good Administrator
 
The_Big_K's Avatar
 
Join Date: 26th November 2005
I'm a Crazy Electrical Engineer
Location: Terra-Firma
Posts: 8,554
Send a message via Yahoo to The_Big_K
Default Re: Write a program to display all perfect numbers between 1 - 100

I'd not ask if I didn't know.
The_Big_K is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)
Old 9th October 2008, 04:26 PM
CE - Newbie
 
c.deepak257's Avatar
 
Join Date: 3rd October 2008
I'm a Crazy computer Engineer
Location: Chennai
Posts: 5
Default Re: Write a program to display all perfect numbers between 1 - 100

perfect number are those in which the whose divisors proper sum is equal to the number.for eg: 6 has 1,2,3 as the proper divisors.sum of 1,2, and 3 is 6.
c.deepak257 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)
Old 14th October 2008, 03:31 PM
CE - Regular Member
 
Join Date: 25th September 2008
I'm a Crazy CSE Engineer
Posts: 65
Thumbs up Re: Write a program to display all perfect numbers between 1 - 100

Hi,

This program finds all perfect numbers within 1 to 100

I have hardoded n = 100!
The inner for loop finds all factors for a number

This is simple way any one would do, but if you think of speed, then you should use euclids formula.



Thanks & Regards
Sriram

Code:
#include <iostream>
using namespace std;
main()
{
    int n = 100,sum = 0;
    cout << "Searching for perfect number less than 100..." << endl;
    for(int num = 1; num <= n; num++)
    {
    sum = 0;
    for(int i = 1; i < num; i++)
    {
       if(!(num%i))
       {
          sum+=i;
       }
    }
    if(sum == num)
        cout <<  num << " Is perfect number" << endl;
    }
}
Quote:
Test run
>./a.out
Searching for perfect number less than 100...
6 Is perfect number
28 Is perfect number
sriramchandrk is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)
Old 17th November 2008, 03:44 PM
CE - Newbie
 
Join Date: 17th November 2008
I'm a Crazy Telecom Engineer
Posts: 9
Default Re: Write a program to display all perfect numbers between 1 - 100

it's funny i was asked this same question a week ago in C and it's been kinda haunting me... the truth of the matter is that i don't understand the maths behind it... i was never math savvy so when it gets to things like these i get confused really easy and my code ... let's not go there.. for now at least
though the question had a different twist, user would enter a number, that creates a table with n amount of rows to be filled by the user with his choice numbers to be tested as perfect numbers, y/n, and the non-perfect numbers need to be multiplied together.

i believe there's another way to do it with modulo (the perfect number calculation itself) but i'm sorta stuck...

here it is
Code:
// loop to fill table with numbers to be checked.
      for (i = 0; i < nbrMax; i++)
          {
          scanf ("%d", &tableNbr[i]);
          }

      // calculations to find perfect numbers
      sum = 0;
      for (j = 0; j < nbrMax; j++)
          {
            for (k = 1; k < tableau[j]; k++)
                {
                    if(tableau[j]%k==0)
                        {
                        sum += k;
                        }
                }
          }
OrAnGeWorX is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)
Old 17th November 2008, 07:08 PM
CE - Regular Member
 
Ashutosh_shukla's Avatar
 
Join Date: 16th November 2008
I'm a Crazy Computer Science & Engineering Engineer
Location: India
Posts: 75
Send a message via Yahoo to Ashutosh_shukla
Default Re: Write a program to display all perfect numbers between 1 - 100

Hi OrAnGeWorX what I could make out of your question is that user enters N numbers and you want the perfect numbers. You also want the product of non perfect numbers.I have tried something I hope you will go through it and reply me.
The code is :

Code:
#include<iostream.h>
#include<conio.h>
#define MAXSIZE 100
void main()
{
 int a[MAXSIZE],n,i,j,sum;
 long int prod=1;
 clrscr();
 cout<<"Enter the number of elements to be checked : ";
 cin>>n;
 cout<<"Enter the elements : ";
 for(i=0;i<n;i++)
  cin>>a[i];
 cout<<"The perfect nos are : \n";
 for(i=0;i<n;i++)
 {
  sum=0;
  for(j=1;j<a[i];j++)
   if(a[i]%j==0)
    sum+=j;
  if(sum==a[i])
   cout<<a[i]<<endl;
  else
   prod*=a[i];
 }
 cout<<"The product of non perfect numbers is : "<<prod;
 getch();
}

Last edited by The_Big_K; 17th November 2008 at 07:12 PM. Reason: Adding [code] tags
Ashutosh_shukla is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)
Old 17th November 2008, 07:18 PM
CE - Enthusiast
 
Raviteja.g's Avatar
 
Join Date: 13th August 2008
I'm a Crazy computer science Engineer
Location: India
Posts: 182
Default Re: Write a program to display all perfect numbers between 1 - 100

what about powerful number?
can you design the program which generates powerful number less than 100
__________________
Innovation isn’t made by hard workers, but by lazier people trying to find easier ways to do the same!Crazy4Technology
Raviteja.g is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)
Old 17th November 2008, 09:29 PM
CE - Newbie
 
Join Date: 17th November 2008
I'm a Crazy Telecom Engineer
Posts: 9
Default Re: Write a program to display all perfect numbers between 1 - 100

Quote:
Originally Posted by Ashutosh_shukla View Post
Hi OrAnGeWorX what I could make out of your question is that user enters N numbers and you want the perfect numbers. You also want the product of non perfect numbers.I have tried something I hope you will go through it and reply me.
The code is :

Code:
#include<iostream.h>
#include<conio.h>
#define MAXSIZE 100
void main()
{
 int a[MAXSIZE],n,i,j,sum;
 long int prod=1;
 clrscr();
 cout<<"Enter the number of elements to be checked : ";
 cin>>n;
 cout<<"Enter the elements : ";
 for(i=0;i<n;i++)
  cin>>a[i];
 cout<<"The perfect nos are : \n";
 for(i=0;i<n;i++)
 {
  sum=0;
  for(j=1;j<a[i];j++)
   if(a[i]%j==0)
    sum+=j;
  if(sum==a[i])
   cout<<a[i]<<endl;
  else
   prod*=a[i];
 }
 cout<<"The product of non perfect numbers is : "<<prod;
 getch();
}
Ashutosh_shukla, thanks for replying.. i should rephrase myself as far as the problem and coding this in C shouldn't be problematic (i guess)

The user enters N for number of integers he'd like to check if or not they are perfect.
stage 1: how many numbers do u want to check?
user enters up to 10 and hits enter
stage 2: program now waits for user to input his n numbers that will be saved in a table.
stage 3: program goes through table[0] to table[n-1] and calculates if integer in each position is perfect or not, display a message accordingly, table[n] = integer is / is not a perfect number and in that same loop calculate the product (multiplication) of the non perfect numbers
finally to display that last number.

Thanks in advance.
Marc
i'll try convertin this to C and see if that works.... having some problems with dev-c++, compiling is fine but when i execute, program is crashing with windows send report window..

Last edited by OrAnGeWorX; 17th November 2008 at 09:31 PM.
OrAnGeWorX is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)
Old 17th November 2008, 09:30 PM
CE - Regular Member
 
Ashutosh_shukla's Avatar
 
Join Date: 16th November 2008
I'm a Crazy Computer Science & Engineering Engineer
Location: India
Posts: 75
Send a message via Yahoo to Ashutosh_shukla
Default Re: Write a program to display all perfect numbers upto 1000

Hi I have got the definition of powerful no from wikipedia as follows:
A powerful number is a positive integer m that for every prime number p dividing m, p^2 also divides m. Equivalently, a powerful number is the product of a square and a cube, that is, a number m of the form m = a^2 * b^3, where a and b are positive integers.

The code that gives powerful nos less than 1000 is :
Code:
#include<iostream.h>
#include<conio.h>
int check_prime(int n)
{
 for(int j=2;j<n;j++)
 {
  if(n%j==0)
  {
   return 0;
  }
 }
 return 1;
}
void main()
{
 int i,j;
 cout<<"the powerful nos upto 100 are : \n";
 for(i=1;i<=1000;i++)
 {
  for(j=2;j<=i;j++)
   if(check_prime(j))
   {
    if((i%j==0 && i%(j*j)!=0)||(i%j!=0 && i%(j*j)==0))
     break;
   }
  if(j>i)
   cout<<i<<"\t";
 }
 getch();
}

Last edited by The_Big_K; 17th November 2008 at 10:17 PM.
Ashutosh_shukla is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


LinkBacks (?)
LinkBack to this Thread: http://www.crazyengineers.com/forum/computer-science-engineering/6116-write-program-display-all-perfect-numbers-between-1-100-a.html
Posted By For Type Date
Algorithm This thread Refback 9th October 2008 12:47 PM
Write a program to display all perfect numbers between 1 - 100 - CrazyEngineers Forum This thread Refback 9th October 2008 11:18 AM

Similar Threads
Thread Thread Starter Forum Replies Last Post
Representing number as sum of smaller numbers uday.bidkar Computer Science & IT Engineering 19 14th July 2009 11:01 AM
Can you guess the numbers? The_Big_K CE - Quiz | Puzzles | Mathematics 120 4th May 2009 09:31 AM
PC Tutorial: Parallel Port Interfacing Techniques - Part 2 xheavenlyx Computer Science & IT Engineering 26 11th January 2009 01:01 AM
Mouse programming in C prabhat kumar Computer Science & IT Engineering 6 28th April 2008 05:57 PM
How to write a Successful Blog, MUST READ... friendster7 Blogs 7 14th April 2008 09:20 PM


All times are GMT +6.5. The time now is 05:23 PM.
Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.3.0
Member comments are owned by the poster. Copyright © 2005-2009 CrazyEngineers.com. All rights reserved.

Advertisements