Perfect numbers, also known as perfect numbers or complete numbers, are some special natural numbers. The sum of all its true factors (that is, divisors other than itself) (that is, the factor function) is exactly equal to itself.
Dim a as Integer,b as Integer,c as IntegerFor a = 1 To 10000c = 0For b = 1 To a / 2If a Mod b = 0 Then c = c + bNext bIf a = c Then Print Str(a)Next a
Attached is the code for the java version
import java.util.ArrayList; public class T013 { public static void main(String[] args){ ArrayList p = new ArrayList(); // Save the decomposition factor of each group int count = 0; // Count (complete) total number) int sum; // sum = (sum of each factor) for(int i=2;i<10000;i++){ // Test each number i sum = 0; for(int j=1;j<i/2+1;j++){ if(i%j==0){ p.add(j); // Save sum of each factor of i += j; // Save sum( The sum of all factors of i) } } if(sum==i){ // If the current i is a perfect number, output (i=the sum of all factors) count++; System.out.printf("%4d" +" is a perfect number, the factor is "+i+"=",i); for(int k=0;k<p.size()-1;k++){ System.out.print(p.get(k)+" +"); } System.out.println(p.get(p.size()-1)); } p.clear(); // At the end of each i, clear the array and recalculate the factors of the next i. } System.out.println("/n found a total of "+count+" perfect numbers!"); } }
The above is the entire content of this article, I hope you all like it.