Algorithm
Problem Name: beecrowd | 1164
Problem Link: https://www.beecrowd.com.br/judge/en/problems/view/1164
Perfect Number
Adapted by Neilor Tonin, URI Brazil
Timelimit: 1
In mathematics, a perfect number is an integer for which the sum of all its own positive divisors (excluding itself) is equal to the number itself. For example the number 6 is perfect, because 1+2+3 is equal to 6. Your task is to write a program that read integer numbers and print a message informing if these numbers are perfect or are not perfect.
Input
The input contains several test cases. The first contains the number of test cases N (1 ≤ N ≤ 100). Each one of the following N lines contains an integer X (1 ≤ X ≤ 108), that can be or not a perfect number.
Output
For each test case print the message “X eh perfeito” (X is perfect) or “X nao eh perfeito” (X isn't perfect) according with to above specification.
Input Sample | Output Sample |
3 |
6 eh perfeito |
Code Examples
#1 Code Example with C++ Programming
Code -
C++ Programming
#include <iostream>
#include <iomanip>
using namespace std;
int main(){
int n;
cin >> n;
int a = 0;
int lista[n];
int numeros[n];
for(int i = 0; i < n; i++){
int num;
cin >> num;
numeros[i] = num;
for(int j = 1; j < num; j++){
if(num%j==0){
a+=j;
}
}
if(a == num){
lista[i] = true;
}
else{
lista[i] = false;
}
a = 0;
}
for(int l = 0; l < n; l++>{
lista[l] == true ? cout << numeros[l] << " eh perfeito" << "\n" : cout << numeros[l] << " nao eh perfeito" << "\n";
}
}
Copy The Code &
Try With Live Editor
Input
Output
#2 Code Example with Javascript Programming
Code -
Javascript Programming
const input = require('fs').readFileSync('/dev/stdin', 'utf8');
const lines = input.split('\n');
let n = Number(lines.shift());
let a = 0;
let lista = [];
let numeros = [];
for(var i = 0; i < n; i++){
let num = Number(lines.shift());
numeros.push(num);
for(var j = 1; j < num; j++){
if (num % j === 0){
a += j;
}
}
if(a === num){
lista.push(true);
}
else{
lista.push(false);
}
a = 0;
}
for(var l = 0; l < n; l++){
lista[l] === true ? console.log(`${numeros[l]} eh perfeito`): console.log(`${numeros[l]} nao eh perfeito`>;
}
Copy The Code &
Try With Live Editor
Input
Output
#3 Code Example with Python Programming
Code -
Python Programming
n = int(input())
a = 0
lista=[]
numeros = []
for i in range(0, n):
num = int(input())
numeros+=[num]
for j in range(1, num):
if num%j==0:
a+=j
if a == num:
lista+=[True]
else:
lista+=[False]
a = 0
for l in range(0, n):
print(f'{numeros[l]} eh perfeito') if lista[l] == True else print(f'{numeros[l]} nao eh perfeito')
Copy The Code &
Try With Live Editor
Input
Output