Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Primes and patterns into 19-digit integers
#1
This is a program I created when very bored to take a list of 19-digit integers from a text file and figure out if they're prime or not. I was trying to get a palindrome out of one of them but failed so far LOL.

The program is listed below but is worthless without the data files which are part of the attachment.

Code: (Select All)
'by mnrvovrfc, first revision Dec-2020
$CHECKING:OFF
_DEFINE A-Z AS LONG
DIM PRIMEBIT(1 TO 4999995) AS _BIT
DIM PVAL(1 TO 1000) AS _UNSIGNED _INTEGER64
DIM X AS _UNSIGNED _INTEGER64, PLIMIT AS _UNSIGNED LONG, Y AS _UNSIGNED LONG, Z AS _UNSIGNED LONG
DIM fl AS _BIT, ofl AS _BIT, q AS _BYTE

dataf$ = makeuserprof$("%DOCU%/prime19dig-pref.txt")
ifile$ = makeuserprof$("%DOCU%/prime19dig.txt")
outf$ = makeuserprof$("%DOCU%/prime19dig-out.txt")

IF NOT _FILEEXISTS(ifile$) THEN
    PRINT "Cannot run without this file: prime20dig.txt"
    END
END IF

IF _FILEEXISTS(dataf$) THEN
    fi = FREEFILE
    OPEN dataf$ FOR INPUT AS fi
    LINE INPUT #fi, a$
    CLOSE fi
    sop = VAL(LTRIM$(a$))
    IF sop < 1 THEN sop = 1
ELSE
    sop = 1
END IF

PRINT "Please wait, loading ..."
READ Y
READ Y
DO WHILE Y
    Y = (Y - 1) \ 2
    PRIMEBIT(Y) = -1
    READ Y
LOOP
PRINT "Checking input file..."
GOSUB setmytitle
fl = -1
l = 1
u = 1
v = UBOUND(PVAL)
fi = FREEFILE
OPEN ifile$ FOR INPUT AS fi
SEEK #fi, sop
DO UNTIL EOF(fi)
    LINE INPUT #fi, a$
    IF a$ <> "" THEN
        PVAL(u) = VAL(a$)
        u = u + 1
        IF fl THEN fl = 0
        IF u > v THEN sop = SEEK(fi): EXIT DO
    END IF
    l = l + 1
LOOP
IF EOF(fi) THEN sop = 1
CLOSE fi
IF fl THEN
    PRINT "Nothing to do!"
    END
END IF

COLOR 15, 5: CLS
GOSUB setmytitle
_DISPLAY
ofl = 0
FOR u = 1 TO v
    X = PVAL(u)
    IF X = 0 THEN EXIT FOR
    IF u MOD 10 = 0 THEN q = _EXIT
    IF q THEN EXIT FOR
    omb = _MOUSEINPUT
    IF omb THEN
        IF _MOUSEBUTTON(1) THEN ofl = -1
        IF _MOUSEBUTTON(2) THEN ofl = -1
    END IF
    IF ofl THEN
        ofl = 0
        _DISPLAY
    END IF
    fl = -1
    Z = 1
    FOR Y = 1 TO 4999995
        Z = Z + 2
        IF PRIMEBIT(Y) THEN
            IF X MOD Z = 0 THEN fl = 0: EXIT FOR
        END IF
    NEXT
    IF fl THEN
        PLIMIT = FIX(SQR(X * 1.0#))
        FOR Y = 9999993 TO PLIMIT STEP 2
            IF X MOD Y = 0 THEN fl = 0: EXIT FOR
        NEXT
    END IF
    IF fl THEN
        PRINT X
        GOSUB setmytitle
        _DISPLAY
    ELSE
        PVAL(u) = 0
    END IF
NEXT

IF q = 0 THEN
    fo = FREEFILE
    IF _FILEEXISTS(outf$) THEN
        OPEN outf$ FOR APPEND AS fo
    ELSE
        OPEN outf$ FOR OUTPUT AS fo
    END IF
    FOR u = 1 TO v
        IF PVAL(u) > 0 THEN
            PRINT #fo, STR$(PVAL(u))
        END IF
    NEXT
    CLOSE fo
    fo = FREEFILE
    OPEN dataf$ FOR OUTPUT AS fo
    PRINT #fo, sop
    CLOSE fo
    COLOR 14
    PRINT "Completed!"
    _DISPLAY
    _DELAY 3
END IF
SYSTEM

setmytitle:
_TITLE "Prime 20 Digits"
RETURN


FUNCTION makeuserprof$ (s$)
    IF s$ = "" THEN
        makeuserprof$ = ""
        EXIT FUNCTION
    END IF
    mypath$ = s$

    ''added NA27
    IF LEFT$(mypath$, 2) = ".\" OR LEFT$(mypath$, 2) = "./" THEN
        afile$ = "qb64curdir.txt"
        b$ = "%USERPROFILE%"
        IF _FILEEXISTS(afile$) THEN
            c$ = ""
            fi = FREEFILE
            OPEN afile$ FOR INPUT AS fi
            IF NOT EOF(fi) THEN
                LINE INPUT #fi, c$
                c$ = RTRIM$(LTRIM$(c$))
            END IF
            CLOSE fi
            IF c$ <> "" THEN
                IF RIGHT$(c$, 1) = "\" OR RIGHT$(c$, 1) = "/" THEN
                    b$ = LEFT$(c$, LEN(c$) - 1)
                ELSE
                    b$ = c$
                END IF
                mypath$ = b$ + MID$(mypath$, 2)
            END IF
        END IF
    END IF

    u = INSTR(mypath$, "/")
    v = INSTR(mypath$, "\")
    IF u = 0 AND v = 0 THEN
        makeuserprof$ = ""
        EXIT FUNCTION
    END IF
    IF u > 0 THEN
        ReplaceString2 mypath$, "/", "\", 0
    END IF
    u = INSTR(mypath$, "%")
    IF u > 0 THEN
        v = INSTR(u + 1, mypath$, "%\")
    ELSE
        u = 0
    END IF
    IF u = 1 AND v > u THEN
        ''continue function code
    ELSE
        makeuserprof$ = mypath$
        EXIT FUNCTION
    END IF
    a$ = UCASE$(mypath$)
    uprof$ = ENVIRON$("USERPROFILE")
    FOR i = 1 TO 6
        SELECT CASE i
            CASE 1
                b$ = "%USERPROFILE%"
                c$ = uprof$
            CASE 2
                b$ = "%USERPROF%"
                ''c$ same as case 1
            CASE 3
                b$ = "%MUSIC%"
                c$ = uprof$ + "\Music"
            CASE 4
                b$ = "%DOCS%"
                c$ = uprof$ + "\Documents"
            CASE 5
                b$ = "%DOCU%"
                c$ = uprof$ + "\Documents"
            CASE 6
                b$ = "%PIC%"
                c$ = uprof$ + "\Pictures"
        END SELECT
        IF INSTR(a$, b$) THEN
            ReplaceString2 mypath$, b$, c$, 1
            EXIT FOR
        END IF
    NEXT
    makeuserprof$ = mypath$
END FUNCTION

FUNCTION returntfn$ (femplate$)
    STATIC sernum AS LONG, sernumset AS _BIT
    sret$ = ""
    a$ = LTRIM$(RTRIM$(femplate$))
    IF a$ <> "" THEN
        u = INSTR(a$, "|")
        IF u > 0 THEN
            IF LEFT$(a$, 1) = "%" OR INSTR(a$, "/") > 0 THEN
                a$ = makeuserprof$(a$)
            END IF
            IF sernumset = 0 THEN
                sernumset = NOT sernumset
                sernum = 1
            END IF
            IF RIGHT$(a$, 1) <> "\" THEN
                DO
                    n$ = LTRIM$(STR$(sernum))
                    IF LEN(n$) < 4 THEN n$ = LEFT$("0000", 4 - LEN(n$))
                    sret$ = a$
                    ReplaceString2 sret$, "|", n$, 1
                    sernum = sernum + 1
                LOOP UNTIL NOT _FILEEXISTS(sret$)
            END IF
        END IF
    END IF
    returntfn$ = sret$
END FUNCTION

SUB ReplaceString2 (tx AS STRING, sfind AS STRING, repl AS STRING, numtimes AS _UNSIGNED LONG)
    DIM s AS STRING, t AS STRING
    DIM ls AS _UNSIGNED LONG, lx AS _UNSIGNED LONG, count AS _UNSIGNED LONG, j AS _UNSIGNED LONG
    IF (tx = "") OR (sfind = "") OR (sfind = repl) OR (LEN(sfind) > LEN(tx)) THEN EXIT SUB
    s = UCASE$(sfind): t = UCASE$(tx)
    ls = LEN(s)
    count = 0
    goahead = 1
    DO
        u = INSTR(t, s)
        IF u > 0 THEN
            tx$ = LEFT$(tx, u - 1) + repl + MID$(tx, u + ls)
            t = UCASE$(tx)
            IF numtimes > 0 THEN count = count + 1: IF count >= numtimes THEN goahead = 0
        ELSE
            goahead = 0
        END IF
    LOOP WHILE goahead
END SUB

'$INCLUDE: 'tprime.bi'

As the program is presented, it looks for a file named "prime19dig.txt" in the user's Documents directory. This file is over 80MB in size! Do not try to open it in Notepad or any other text editor that cannot handle files larger than 1MB. I created it and a few others with a Lua script, being more interested in patterns of the digits. It's because trying to go sequentially would be very slow and come up with numbers boring to look at or could peel the paint from the walls. Such as 10000000002835706257.

This program only takes numbers from a list and figures out if they're prime or not. If prime they are listed on the screen and they are also sent to an output file called "prime19dig-out.txt". The program should end in 5 to 10 minutes on a dual-core CPU laptop created within the last five years. Then the user could choose to run it again to pick up where it left off. This could be done for a while until it runs out of "candidates" and it starts from the beginning -- then you say give me another 80MB file. This information is saved in a file called "prime19dig-pref.txt", should not be tampered by anybody who hasn't read and understood the source code!

I have indeed created another version of this program that focused only on numbers higher than 18 followed by 18 zeros. But the program posted here is only one example of what could be done with 64-bit integers. Imagine when 128-bit becomes the norm later on...

Note: there was another number 10043560040404043561 that figured in one of my fantasy stories, about a rock band that created a song singing it out. It was supposed to be for LOL's. Imagine like Katy Perry or Springsteen singing, "zero zero four, zero four, zero four, zero four, three five six!" Sorry not a good judge with female solo artists after Madonna and others ruined my sense for pop-rock forever. But that integer is not prime, must add six to it. Wink

(Maybe I should use an external file-sharing service because I made one attempt to upload but it didn't display in the available list of attachments, although it reflected the large jump in size of total attachments. I will have to delete one of the two attempts later, if it could be found.)


.zip   prime19dig-by-mnrvovrfc.zip (Size: 8.64 MB / Downloads: 47)
Reply


Messages In This Thread
Primes and patterns into 19-digit integers - by mnrvovrfc - 03-07-2023, 04:58 PM



Users browsing this thread: 2 Guest(s)