Scala programming (2) ---- maximum Common Divisor

Source: Internet
Author: User

GCD. Scala

Object GCD {def main (ARGs: array [String]) {println (gcd1 (ARGs (0 ). toint, argS (1 ). toint) println (gcd2 (ARGs (0 ). toint, argS (1 ). toint)} def gcd1 (X: int, Y: INT): Int = {var A = x var B = y while (! = 0) {var temp = A = B % a B = temp} B} def gcd2 (X: int, Y: INT ): int = {If (x = 0) y else gcd2 (Y % x, x )}}

$ Scalac GCD. Scala

$ Scala GCD 12 21

$ Scala GCD 21 12

 

Gcd1 adopts iteration, gcd2 adopts recursion, and gcd1 and gcd2 have basically been the simplest.

To make it easier to remember, express the thinking process:

 

Scala programming (2) ---- maximum Common Divisor

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.