Using a while loop to compute 1+1/2!+1/3!+...+1/20!

Source: Internet
Author: User
Package practice;
  /* Use while loop to compute 1+1/2!+1/3!+...+1/20! A is used to store one of the first n factorial points sum is used to accumulate and/or public class Whiledemo {The public static void main (string[] args) {/*i=i+1 is a short form of i+=1;
Qualitative difference: If I is the short type, + =: automatic type promotion, and = will not be so short i=0;
 i+=1;//compilation passed, type elevation i=i+1;//compilation failed because this is the assignment operation, I is the short type, and 1 is the type int by default/double = 0,a = 1;//because it is fractional, so the double type int i = 1;
 while (i <=) {sum +=a;
 i++;
 A*= (1.0/i);//Make a value change System.out.println (a*i);
 } System.out.println (sum);
 System.out.println ("_________________________");
 Fordemo.main (args);
System.out.println (Text.jiecheng (4)); } class fordemo{public static void Main (string[] args) {double = 0,a = 1; for (int i = 1; I <=20; i++) {sum
+=a;
 A*= (1.0/i);
System.out.println (A*i);
} sum--;
SYSTEM.OUT.PRINTLN (sum); }//Recursive procedure: Inefficient, judgment affects performance class text{public static void Main (string[] args) {//double sum=0.0,count=1.0; (count<=20.0)
    {//sum+=1.0/(Jiecheng (count));//count++;//}//System.out.print (sum);
     }
      public static long Jiecheng (long sum) {/* for (int i=1;i<=c;i++) {sum*=i;
 }*/if (sum<=1) return 1;
 Return Sum*jiecheng (sum-1);
 }
}


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.