Author Topic: A couple of classic examples in Euphoria  (Read 1544 times)

Tomaaz

  • Guest
A couple of classic examples in Euphoria
« on: September 06, 2018, 06:14:56 PM »
Fast algorithm for counting primes

Code: [Select]
include std/convert.e
function main()
    sequence liczby = repeat(0, 100000000)
    for x = 3 to length(liczby) by 2 do
        if liczby[x] = 0 then
            for y = x * x to length(liczby) by x do
                liczby[y] = 1
            end for
        end if
    end for
    object plik = open("pierwsze.txt", "w")
    for x = 3 to length(liczby) by 2 do
        if liczby[x] = 0 then
            puts(plik, to_string(x) & " ")
        end if
    end for
    close(plik)
    return 0
end function

main()

Calculating Pi

Code: [Select]
include std/math.e
include std/text.e

function main()
    atom n = 35001, a = 10000, d, g, k, e = 0
    sequence f = repeat(2000, n)
   
    for c = n to 14 by -14 do
        d = 0
        for b = c to 1 by -1 do
            d = d * b
            g = b * 2 - 1
            d = d + f[b] * a
            f[b] = mod(d, g)
            d = floor(d / g)
            k = e + floor(d / a)
        end for
        sequence k2 = "000000" & sprint(k)
        puts(1, k2[(length(k2) - 3)..(length(k2))])
        e = mod(d, a)
    end for
    return 0
end function

main()

Counting words in a text file

Code: [Select]
include std/sequence.e
include std/convert.e
include std/text.e
include std/sort.e
include std/io.e

function byilosc(object a, object b)
    return compare(b[2], a[2])
end function

function sprawdz (object x, object y)
    if find(x, "qwertyuiopasdfghjklzxcvbnm ") then
        return x
    else
        return 32
    end if
end function

function main()
    integer y, l = 0
    sequence unikalne = {}
    sequence pary = {}
    puts(1, "Reading the file...\n")
    sequence calosc = lower(read_file("warandpeace.txt"))
    puts(1, "Counting...\n")
   
    object tresc = apply(calosc, routine_id("sprawdz"))
   
    sequence wszystkie = split(tresc, " " )
   
    for x = 1 to length(wszystkie) do
        if length(wszystkie[x]) > 1 or equal(wszystkie[x], "a") or equal(wszystkie[x], "i") then
            y = find(wszystkie[x], unikalne)
            if y > 0 then
                unikalne[y + 1] = unikalne[y + 1] + 1
            else
                unikalne = append(unikalne, wszystkie[x])
                unikalne = append(unikalne, 1)
            end if
            l = l + 1
        end if
    end for
   
    puts(1, "Sorting and writing the result to the file...\n")
    for x = 1 to length(unikalne) by 2 do
        pary = append(pary, {unikalne[x], unikalne[x + 1]})
    end for
   
    object plik = open("words.txt","w")
    puts(plik, "All words: " & sprint(l) & "\n")
    puts(plik, "Unique words: " & sprint(length(pary)) & "\n\n")
   
    sequence posortowane = custom_sort( routine_id("byilosc"), pary)
   
    for x = 1 to length(posortowane) do
        puts(plik, to_string(posortowane[x][1]) & " - " & sprint(posortowane[x][2]) & "\n")
    end for
    close(plik)
    puts(1, "Done!\n")
    return 0
end function

main()

Recursively counting files in a directory

Code: [Select]
include std/text.e
include std/console.e
include std/filesys.e

integer x = 0, y = 0, z = 0, w = 0, p = 0, s = 0, v = 0, t = 0, b = 0
sequence e

function pisz(sequence nazwa, sequence plik)
puts(1, nazwa & plik[D_NAME] & "\n")
if equal(plik[D_ATTRIBUTES], "d") then
x = x + 1
else
y = y + 1
e = lower(fileext(plik[D_NAME]))
if equal(e, "jpg") then
z = z + 1
elsif equal(e, "png") then
w = w + 1
elsif equal(e, "mp4") then
p = p + 1
elsif equal(e, "mov") then
s = s + 1
elsif equal(e, "avi") then
v = v + 1
elsif equal(e, "tiff") then
t = t + 1
elsif equal(e, "bmp") then
b = b + 1
end if
end if
return 0
end function

function main()
    object res = walk_dir("/home/", routine_id("pisz"), 1)
    puts(1, "Number of directories: " & sprint(x) & "\n")
    puts(1, "Number of files: " & sprint(y) & "\n\n")
    puts(1, sprint(z) & " jpg files" & "\n")
    puts(1, sprint(w) & " png files" & "\n")
    puts(1, sprint(t) & " tiff files" & "\n")
    puts(1, sprint(b) & " bmp files" & "\n")
    puts(1, sprint(v) & " avi files" & "\n")
    puts(1, sprint(s) & " mov files" & "\n")
    puts(1, sprint(p) & " mp4 files" & "\n")
    return 0
end function

main()

I've also started two simple projects in Euphoria. First one is a simple graphics library written (almost) in pure Euphoria. At the moment it can only create an image, draw pixels in it and save it to a PNG file. I use convert from ImageMagick to save files in compressed formats (it's beyond my skills and knowledge to deal with these formats directly in pure Euphoria).

Library

Code: [Select]
namespace img
include std/sequence.e
include std/convert.e
include std/error.e
include std/filesys.e

public function create_image(atom x, atom y, sequence kolor)
    sequence tymczasowe = repeat(kolor, x)
    return repeat(tymczasowe, y)
end function

public function save_image(sequence nazwa, sequence plik)
    object obrazek = open(to_string(plik) & ".ppm", "w")
    puts(obrazek, "P3\n")
    puts(obrazek, to_string(length(nazwa[1])) & " " & to_string(length(nazwa)) & "\n")
    puts(obrazek, "255\n")
    for y = 1 to length(nazwa) do
        for x = 1 to length(nazwa[1]) do
            puts(obrazek, to_string(nazwa[y][x][1]) & " " & to_string(nazwa[y][x][2]) & " " & to_string(nazwa[y][x][3]) & " ")
        end for
        puts(obrazek, "\n")
    end for
    close(obrazek)
    system("convert " & to_string(plik) & ".ppm " & to_string(plik) & ".png")
    delete_file(to_string(plik) & ".ppm")
    return 0
end function

public function point(sequence nazwa, atom x, atom y, sequence kolor)
    if x > length(nazwa[1]) or y > length(nazwa) then
        puts(1, "Wrong coordinations!")
    else
        nazwa[y][x][1] = kolor[1]
        nazwa[y][x][2] = kolor[2]
        nazwa[y][x][3] = kolor[3]
    end if
    return nazwa
end function

Example

Code: [Select]
include img.e
include std/math.e

function main(atom szer, atom wys)
    object mandelbrot = create_image(szer, wys, {0, 0, 0})
    atom przelx, przely, x2, y2, x3, a, b, c, z, a2, b2
    przelx = 3 / szer
    przely = 2 / wys
    for x = 1 to szer do
        for y = 1 to wys do
            c = 0
            a = 0
            b = 0
            z = 0
            x2 = (przelx * x) - 2
            y2 = (przely * y) - 1
            while c < 255 and z < 4 do
                a2 = a * a - b * b
                b2 = 2 * a * b
                a = a2 + x2
                b = b2 + y2
                z = a * a + b * b
                c = c + 1
            end while
            if c = 255 then
                mandelbrot = point(mandelbrot, x, y, {0, 0, 0})
            else
                mandelbrot = point(mandelbrot, x, y, {255 - c, mod(c, 50) * 5, c})
            end if
        end for
        ? x
    end for
    save_image(mandelbrot, "mandelbrot")
    return 0
end function
   
main(900, 600)

The second project is a simple internet radio player. For this one I'm gonna use EuGTK and fmedia. Both projects are Linux only. It's possible that they will be working on Windows, but I don't care about it.
« Last Edit: September 06, 2018, 06:17:58 PM by Tomaaz »