init ps6
This commit is contained in:
20
exercises/06/vslc/vsl_programs/ps6-codegen2/euclid.vsl
Normal file
20
exercises/06/vslc/vsl_programs/ps6-codegen2/euclid.vsl
Normal file
@@ -0,0 +1,20 @@
|
||||
func euclid ( a, b )
|
||||
begin
|
||||
if a < 0 then a := -a
|
||||
if b < 0 then b := -b
|
||||
if gcd ( a, b ) > 1 then
|
||||
print "Greatest common divisor of", a, "and", b, "is", gcd ( a, b )
|
||||
else
|
||||
print a, "and", b, "are relative primes"
|
||||
return 0
|
||||
end
|
||||
|
||||
func gcd( a, b )
|
||||
begin
|
||||
var g
|
||||
if b > 0 then
|
||||
g := gcd ( b, a - ((a/b)*b) )
|
||||
else
|
||||
g := a
|
||||
return g
|
||||
end
|
||||
21
exercises/06/vslc/vsl_programs/ps6-codegen2/newton.vsl
Normal file
21
exercises/06/vslc/vsl_programs/ps6-codegen2/newton.vsl
Normal file
@@ -0,0 +1,21 @@
|
||||
// Approximate square root by the Newton/Raphson method for f(x) = x^2 - n
|
||||
// f(x) = x^2 - n = 0
|
||||
// f'(x) = 2x
|
||||
// x{n+1} = x{n} - (x^2-n) / 2x
|
||||
|
||||
func newton ( n )
|
||||
begin
|
||||
print "The square root of", n, "is", improve ( n, 1 )
|
||||
return 0
|
||||
end
|
||||
|
||||
func improve ( n, estimate )
|
||||
begin
|
||||
var next
|
||||
next := estimate - ( (estimate * estimate - n) / ( 2 * estimate ) )
|
||||
if next - estimate = 0 then
|
||||
// Integer precision converges at smallest int greater than the square
|
||||
return next-1
|
||||
else
|
||||
return improve ( n, next )
|
||||
end
|
||||
23
exercises/06/vslc/vsl_programs/ps6-codegen2/while_test.vsl
Normal file
23
exercises/06/vslc/vsl_programs/ps6-codegen2/while_test.vsl
Normal file
@@ -0,0 +1,23 @@
|
||||
// This program is a simple test of while loops, counting down from 19 to 0
|
||||
// and skipping 10 (if continue is implemented)
|
||||
|
||||
func while_test ()
|
||||
begin
|
||||
var a
|
||||
a := 20
|
||||
print a
|
||||
if a > 0 then print "foobar"
|
||||
while a > 0 do
|
||||
begin
|
||||
if a = 10 then
|
||||
begin
|
||||
a -= 1
|
||||
print "Skip..."
|
||||
continue
|
||||
end
|
||||
else
|
||||
a -= 1
|
||||
print a
|
||||
end
|
||||
return 0
|
||||
end
|
||||
Reference in New Issue
Block a user