Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.forth > #10531

Sum of squares

From mhx@iae.nl (Marcel Hendrix)
Subject Sum of squares
Newsgroups comp.lang.forth
Message-ID <10851509008435@frunobulax.edu> (permalink)
Date 2012-03-26 20:50 +0200
Organization Wanadoo

Show all headers | View raw


Admit it (when trying it yourself), this exercise is 
surprisingly tricky. 

-marcel

-- -------------------------------------------
(*
 * LANGUAGE    : ANS Forth with extensions
 * PROJECT     : Forth Environments
 * DESCRIPTION : Sum of squares of two largest of three values
 * CATEGORY    : Programming exercise 
 * AUTHOR      : Marcel Hendrix 
 * LAST CHANGE : March 25, 2012, Marcel Hendrix 
 *)



	NEEDS -miscutil

	REVISION -sumsquares "--- Sum of Squares      Version 0.01 ---"

	PRIVATES

DOC
(*

  Sum of squares of two largest of three values

  This exercise comes to us from the book "Structure and Interpretation of 
  Computer Programs" by Abelson and Sussman (exercise 1.3):
  
  Define a procedure that takes three numbers as arguments and returns 
  the sum of the squares of the two larger numbers.
*)
ENDDOC

: DSQR ( u -- du ) DUP M* ; PRIVATE
: .ANSWER ( ud -- ) CR ." The sum of the squares of the two larger numbers is " (n,3) ; PRIVATE

: SOS ( n1 n2 n3 -- )
	2DUP > IF  SWAP  ENDIF ( x1) DSQR 2>R
	MAX ( x2) DSQR 2R> D+ .ANSWER ;


: SQR ( u1 -- u2 ) DUP * ; PRIVATE
: SUM-OF-SQUARES ( n1 n2 n3 -- ) 2DUP > IF  SWAP  ENDIF  SQR -ROT MAX SQR + . ;

:ABOUT	CR ." Print the sum of the squares of the two larger numbers from {n1, n2, n3}."
	CR ." The numbers should have less than 18 digits."
     	CR ." Try: 1 2 3 SOS            -- print 13, double precision." 
	CR ."      1 2 3 SUM-OF-SQUARES -- single precision." ;

		.ABOUT -sumsquares CR
		DEPRIVE

                              (* End of Source *)

Back to comp.lang.forth | Previous | NextNext in thread | Find similar


Thread

Sum of squares mhx@iae.nl (Marcel Hendrix) - 2012-03-26 20:50 +0200
  Re: Sum of squares "A. K." <akk@nospam.org> - 2012-03-26 21:43 +0200
  Re: Sum of squares Paul Rubin <no.email@nospam.invalid> - 2012-03-26 16:52 -0700
    Re: Sum of squares Helmar Wodtke <helmwo@gmail.com> - 2012-03-27 03:15 -0700
      Re: Sum of squares Paul Rubin <no.email@nospam.invalid> - 2012-03-28 08:16 -0700
    Re: Sum of squares BruceMcF <agila61@netscape.net> - 2012-03-28 06:06 -0700
  Re: Sum of squares Zbiggy <zbigniew2011REMOVE@gmail.REMOVE.com> - 2012-03-27 12:13 +0100
    Re: Sum of squares Helmar Wodtke <helmwo@gmail.com> - 2012-03-27 04:03 -0700
      Re: Sum of squares Zbiggy <zbigniew2011REMOVE@gmail.REMOVE.com> - 2012-03-27 13:55 +0100
  Re: Sum of squares Albert van der Horst <albert@spenarnc.xs4all.nl> - 2012-03-27 11:05 +0000
    Re: Sum of squares Albert van der Horst <albert@spenarnc.xs4all.nl> - 2012-03-28 11:41 +0000
      Re: Sum of squares Paul Rubin <no.email@nospam.invalid> - 2012-03-28 07:41 -0700
      Re: Sum of squares mhx@iae.nl (Marcel Hendrix) - 2012-03-29 22:06 +0200
      Re: Sum of squares Albert van der Horst <albert@spenarnc.xs4all.nl> - 2012-03-31 11:48 +0000
    Re: Sum of squares Alexander Adler <alexander.adler@stud.uni-frankfurt.de> - 2012-03-29 19:33 +0200
      Re: Sum of squares Helmar Wodtke <helmwo@gmail.com> - 2012-03-29 11:16 -0700
        Re: Sum of squares Albert van der Horst <albert@spenarnc.xs4all.nl> - 2012-04-03 09:47 +0000
    Re: Sum of squares Helmar Wodtke <helmwo@gmail.com> - 2012-03-28 09:42 -0700
    Re: Sum of squares Helmar Wodtke <helmwo@gmail.com> - 2012-03-28 11:08 -0700
  Re: Sum of squares Ecki <ecki@intershop.de> - 2012-03-27 16:15 +0200
    Re: Sum of squares Helmar Wodtke <helmwo@gmail.com> - 2012-03-27 07:35 -0700
      Re: Sum of squares hwfwguy@gmail.com - 2012-03-27 08:00 -0700
        Re: Sum of squares Helmar Wodtke <helmwo@gmail.com> - 2012-03-27 08:21 -0700
        Re: Sum of squares Ecki <ecki@intershop.de> - 2012-03-29 09:05 +0200

csiph-web