Basic for the perfect number within 10000 _VB

Source: Internet
Author: User

The complete number (Perfect number), also known as the perfect or complete number, is a special natural number. The sum of all its true factors (that is, divisors other than itself) and (that is, the factor function) is exactly equal to itself.

Dim A as integer,b as integer,c as Integer for
a = 1 to 10000
c = 0 for
B = 1 to a \ 2
If a Mod b = 0 Then c = C + b
next b
If a = c Then Print Str (a)
next

Attach the Java version of the code

Import java.util.ArrayList; 
public class T013 {public 
  static void Main (string[] args) { 
    ArrayList p = new ArrayList ();//save decomposition Factor 
    int Co for each group UNT = 0; Count (total number of ends) 
    int sum;  sum = (and per 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 I for each factor 
          sum = j;  Save sum (all factor of I Plus and) 
        } 
      } 
      if (sum==i) {   //If the current I is finished, output (i= all factor sum) 
        count++; 
        System.out.printf ("%4d" + "is the end of the 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 (); Each I ends, empties the array, and the next I factor is weighed. 
    } 
    System.out.println ("\ n Find the +count+"!) "); 
  } 

The above mentioned is the entire content of this article, I hope you can enjoy.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.