Programming The RSS feed for Programming.

  • Testing primality with Free Pascal

    I refined my Free Pascal program to accept 64-bit unsigned integers, which allows up to 19 decimal digits. However, testing with Big Primes, it is only usable up to 15 decimal digits on my Raspberry Pi 4. Any bigger primes take forever to test, in human perspective. The problem seems to be in how primes are generated in the primality test. The prime number generator uses a simple algorithm of odd numbers greater than one, and less than the square root of of the number of which its primality is tested. For small numbers it’s fine, but for larger numbers testing with odd numbers instead of actual prime numbers is a bit of waste of precious CPU time.

    Still, being able to test primality with a relatively simple algorithm is rather awesome as a first attempt.

    The slow-down in the algorithm is not caused by testing primality, but by the inefficiency of generating prime numbers. So I need a faster algorithm to do just that. I’ve looked around, but it requires me to learn new concepts in mathematics. I could do that, or, come back to it later when I’m more versed in writing in Pascal, or even stop here, since I don’t really have any incentives to create a full-blown efficient prime number generator, in other words, reinvent the wheel.

    program isPrime3;
    uses crt;
    
    const
      zero  : qword = 0;
      one   : qword = 1;
      two   : qword = 2;
      three : qword = 3;
    
    var
      num   : qword;
      again : char;
      
    function isprime(num : qword) : boolean;
    var
      iscomposite : boolean;
      test        : qword;
    
    begin
      if num = one then 
        isprime := false
      else 
        if (num = two) or (num = three) then
          isprime := true
        else
          if (num mod two) = zero then
            isprime := false
          else begin
            iscomposite := false;
            test     := three;
            while (test * test) <= num do begin
              if (num mod test) = zero then
                iscomposite := true;
              test := test + two;
            end;
            isprime := not iscomposite;
          end;
    end;
    
    begin
      again := 'Y';
      while (again ='y') or (again = 'Y') or (again = chr(13)) do begin
        num := zero;
        while num < one do begin
          write('Give a whole positive number: ');
          readln(num);
        end;
        if isprime(num) then
          writeln(num, ' is prime')
        else
          writeln(num, ' is not prime');
        write('again? [Y/n] ');
        again := readkey;
        writeln(again);
      end;
    end.
    

    P.S. I fixed a bug that caused a press on the Enter key to exit the program, instead of continuing it as I meant it to do. Checking additionally for the Enter key chr(13) was easy enough. There are still bugs, though, but it’s usable enough for now, as throw-away code, not meant for application in a realworld app.

    P.P.S.S. You can test this code on TutorialsPoint, which has a Pascal IDE, you could type the source code in, or copy and paste it from this article.

  • I refined yesterday’s Pascal program to have somewhat more functionality, and found a way to turn the contents of a terminal window into a PNG. It is a bit unwieldy, though. I’m curious how Timeline will possibly balk on this. I suppose @manton will have to tweak things a bit.

    The image shows a code editor containing a Pascal program that checks if a number is prime, with highlighted comments and test outputs.
  • More Pascal programming in Free Pascal on Raspberry Pi OS. Apparently, the robot understands programming, since the alt description is spot on.

    A terminal window on a Raspberry Pi is displaying a program written in Pascal to test if numbers are prime, with examples of its execution for the numbers 3 and 1021.
  • Remember Pascal? It’s still around. Here I’m running Free Pascal in a RaspberryPi OS terminal window.

    A terminal window displays a user compiling and running a Pascal program called hello.pas on a Raspberry Pi, resulting in the output Hello world!.
  • I’m going to ignore Nvidea’s CEO charged statement people shouldn’t learn how to code anymore. I think BBC Basic is an excellent language for someone like myself, AI be damned!

    A scenic mountain landscape serves as the desktop background for a computer screen, with several open folders and text editing programs displayed.
  • A “little” thing as a broken iPad won’t stop me from making art. I made this when the iPad was almost gone.

    simple animated GIF showing a cat and a pirate

    And the other piece I made with a PETSCII editor on my RPi, based on a piece I did earlier this month.

    PETSCII art of Boys Will Be Boys

    Not having a backup for my Raspberry Pi makes me a bit nervous, as it should
    πŸŽ¨πŸ±πŸ‘¨β€πŸ’»πŸ•ΉοΈπŸ‘Ύ

  • This was taxing my iPad. Youtube in Safari with adblocker as picture in picture on top of a drawing app. The strokes had to be placed tactically to allow for overall sluggishness. Also, very distractive drawing and watching a video.
    πŸŽ¨πŸ‘¨β€πŸ’»πŸ“Ί

    screenshot of drawing in ibisPaint X with a Youtube video floating above it
  • As Hanna-Barbera cartoons of the eighties have taught us, cats can without most clothes, except the most essential, like a hat or a cane.
    πŸŽ¨πŸ±πŸ‘¨β€πŸ’»πŸ•ΉοΈπŸ‘Ύ

    Commodore 64 multi-color bitmap painting of an antromorphized cat behind a pile of clothes
  • I find it always such a surprise how a pixel art drawing I made subtly (or sometimes radically) changes to conform to the rigors of the C64 multicolor bitmap.

    Other than that, I used some of the edging techniques (sharp, lost and soft edges). So there’s that too. Happy 😊 camper here!
    πŸŽ¨πŸ‘½πŸ‘¨β€πŸ’»πŸ•ΉοΈπŸ‘Ύ

    Commodore 64 multipaint drawing of an anime version of Rick Deccard
  • Searching on Flickr for Creative Commons photos of kittens I found this cute black and white one. I used it as reference for my C64 hi-res bitmap pixel art drawing.

    Sometimes less color is better. Especially if the one depicted is staring into your soul.
    πŸŽ¨πŸ‘¨β€πŸ’»πŸ‘ΎπŸ•ΉοΈπŸ±πŸ˜ˆ

    Commodore 64 hi-res pixel art drawing of a black and white kitten
  • I didn’t get to experiment with edges. Still, I drew something I love, so that’s good.

    Maybe advice from an oil painter doesn’t translate all that well to pixel art. Pixel art has more in common with decorative art (like embroidery) than with traditional art you can hang on a wall.
    πŸŽ¨πŸ±πŸ‘¨β€πŸ’»πŸ•ΉοΈπŸ‘Ύ

    Commodore 64 multipaint illustration of two kittens, one sniffing a flower
  • Public domain now?
    πŸŽ¨πŸ‘¨β€πŸ’»πŸ•ΉοΈπŸ‘Ύ

    Commodore 64 multicolor illustration of a cartoon mouse holding a piece of cheese and a drink animated GIF of drawing process
  • I suppose the lesson to be learned here is:

    Don’t look into the spiral, or you’ll go cuckoo, bananas, Dada

    A quick peek is no problem, though.
    πŸŽ¨πŸ‘¨β€πŸ’»πŸ•ΉοΈπŸ‘Ύ

    Commodore 64 hires bitmap image showing a spiral and a confused artist
  • Another day, another cat drawing. This time our more blocky version of a feline fellow creature. Sketch in ibisPaint X and final drawing in Commodore 64 multicolor bitmap (160 x 200 resolution, 16 colors). If I could, I’d knit him a cozy sweater.
    πŸ±πŸŽ¨πŸ‘¨β€πŸ’»πŸ•ΉοΈπŸ‘Ύ

    illustration of a stylized cat with chunk in blocked letters written underneath Commodore 64 multicolor bitmap image of a chunky cat illustration
  • I think I’m starting to grasp how to draw in C64 multicolor. You get three colors that can be used anywhere, and per 8 by 8 pixels block you get to pick another color out of 16 possible colors. Careful placement of the pixels is key. If you do it right, you can have a very colorful image
    πŸŽ¨πŸ‘¨β€πŸ’»πŸ•ΉοΈπŸ‘Ύ

    cheeky multicolor illustration on the Commodore 64, stating that boys will be boys
  • Another day, another cat drawing for the old Commodore 64
    😻 🎨 πŸ‘¨β€πŸ’» πŸ•ΉοΈ πŸ‘Ύ

    Commodore 64 multicolor drawing of a blue cat on a red background
  • I reworked an older self-portrait that I made last year as a pixel art drawing, and turned it into an image on the Commodore 64. I like that the limited color palette doesn’t seem to limit me as much as it used to. It takes some effort (well, a lot of effort), but I think it was worth the time.
    🎨 πŸ‘¨β€πŸ’» πŸ•ΉοΈ πŸ‘Ύ

    Commodore 64 multicolor selfie
  • Here are some of the steps I took to create a multicolor pixel art drawing of a cat.

    grayscale image of a cat drawn in ibisPaint X

    drawn in ibisPaint X in 4 colors

    colored pixel art of the same cat in Pixaki

    traced and redrawn in Pixaki

    colored pixel art of the cat display in VICE C64

    made into C64 runnable multicolor art with MultiPaint, loaded into VICE C64

    It’s a process, taking many hours to complete. I think I’m improving.

    πŸ‘¨β€πŸ’»πŸŽ¨

  • Making room for two text screens, so I can use them for buffering is great, but running it from C64 Basic is one ugly hack, and slow too. But I’ve done it, which is great, since I learned a lot from it, and that was the whole point here. The code I keep, in case I need (some of) it πŸ‘¨β€πŸ’»

    screenshot of Commodore 64 Basic code in VS Code
  • I fired up my art app and sculpted on a canvas the size of a retro-computer’s text character (8 pixel wide and 8 pixels high). I had to struggle, because the app wanted it to look nicer than it really was.

    I suppose you can already guess what it represents. You can do a lot in 64 bits of data πŸ‘¨β€πŸ’»πŸŽ¨

    8 by 8 sprite of a black cat animated GIF of drawing process of an 8 by 8 sprite of a black cat