Seite 2 von 2

Re: Kreisberechnungen

Verfasst: Mittwoch 31. Oktober 2012, 14:23
von jerch
Python kann auch mit Kreisen :twisted: :

Code: Alles auswählen

import math

def approximate_circle_area(d, iterations):
    r = d/float(2)
    A = r**2 * 2
    x = [math.sqrt(2 * r**2) / 2]
    def step(r, iterations):
        for i in xrange(iterations):
            a = x[0]
            b = r - math.sqrt(r**2 - a**2)
            x[0] = math.sqrt(a**2 + b**2) / 2
            yield a * b / 2 * 2**(i+3)
    return A + sum(step(r, iterations))

if __name__ == '__main__':
    d = 2
    print approximate_circle_area(d, 20), math.pi

Re: Kreisberechnungen

Verfasst: Mittwoch 31. Oktober 2012, 14:25
von sparrow
Ich wette bei den nächsten 10 Postes wird eine Brainfuck-Lösung dabei sein :)

Re: Kreisberechnungen

Verfasst: Mittwoch 31. Oktober 2012, 15:04
von BlackJack

Code: Alles auswählen

#!/usr/bin/env io

"Bitte Durchmesser eingeben: " print
diameter := File standardInput readLine asNumber
if(diameter isNan,
    "Eingabe war keine Zahl.",
    "Die Fläche beträgt: " .. (Number constants pi * diameter squared / 4)
) println

Re: Kreisberechnungen

Verfasst: Mittwoch 31. Oktober 2012, 22:49
von StefanLawl
BlackJack hat geschrieben:

Code: Alles auswählen

#!/usr/bin/env io

"Bitte Durchmesser eingeben: " print
diameter := File standardInput readLine asNumber
if(diameter isNan,
    "Eingabe war keine Zahl.",
    "Die Fläche beträgt: " .. (Number constants pi * diameter squared / 4)
) println
Ist das Pascal? :mrgreen:

Re: Kreisberechnungen

Verfasst: Mittwoch 31. Oktober 2012, 23:00
von BlackJack
@StefanLawl: Nee, Pascal (+ Assembler) war schon auf der ersten Seite. Das ist Io, wie man an der ersten Zeile erkennen kann.

Und das hier ist 6510-Assembler der Gebrauch von den Gleitkommazahl-Routinen und -Konstanten im C64-BASIC-ROM macht:

Code: Alles auswählen

        !to "kreis.prg", cbm
        
        i               = $02
        cursor_flag     = $cc
        
        facout          = $aabc
        strout          = $ab1e
        str2fac         = $b7b5
        fac_mul         = $ba28
        arg_fac_mul     = $ba2b
        fac2arg         = $bc0c
        chrout          = $ffd2
        chrget          = $ffe4
        
        pi              = $aea8
        one_fourth      = $e2ea
        
        * = $0801

basicstart
        !wo next_line
        !wo 2012        ; BASIC line number.
        !by $9e         ; SYS token.
        !pet "2061"     ; Machine code start address.
        !by 0           ; BASIC line end.
next_line
        !by 0, 0        ; BASIC program end.

;--------------------------------------
!zone
        lda #<diameter_txt
        ldy #>diameter_txt
        jsr strout
        
        lda #0                  ; Cursor on.
        sta cursor_flag
        sta i                   ; Input to `buffer`.
L1
        jsr chrget
        beq L1
        cmp #13
        beq L2
        ldx i
        sta buffer,x
        inc i
        jsr chrout
        jmp L1
L2
        lda #1                  ; Cursor off.
        sta cursor_flag

        lda #<area_txt
        ldy #>area_txt
        jsr strout

        lda #<buffer            ; Input to FAC.
        sta $22
        lda #>buffer
        sta $23
        lda i
        jsr str2fac
        
        jsr fac2arg             ; FAC := FAC * FAC
        lda $61
        jsr arg_fac_mul
        
        lda #<pi                ; FAC := π * FAC
        ldy #>pi
        jsr fac_mul
        
        lda #<one_fourth        ; FAC := 0.25 * FAC
        ldy #>one_fourth
        jsr fac_mul

        jsr facout              ; Print FAC.
        lda #13
        jmp chrout

;--------------------------------------
diameter_txt
        !pet "durchmesser? ", 0
area_txt
        !pet " ", 13, "flaeche:", 0
;--------------------------------------
buffer
Das assemblierte Programm ist genau 128 Bytes gross. :-)