Zahl kleiner machen durch ZAHL % PRIMZAHL

Stellt hier eure Projekte vor.
Internetseiten, Skripte, und alles andere bzgl. Python.
Antworten
Benutzeravatar
Sr4l
User
Beiträge: 1091
Registriert: Donnerstag 28. Dezember 2006, 20:02
Wohnort: Kassel
Kontaktdaten:

Ziel: große Zahl durch zwei große Primzahlen teilen und den Rest übermitteln damit ein Script aus den zwei Zahlen wieder die große Zahl errechnen kann.
Soviel zur Theorie ich weiß das der Code jenseits von Gut und Böse ist.

Ich wollte auch nur eine Funktionellen Code habe ihn auch versucht nach C zu portieren. Falls mal jemand mal drüber gucken möchte, bitte Tipps äußern bzw auch posten besonders Wege um das Python Programm efektivere zu machen.

Ich weiß auch das drei Primzahlen genauer sind zwei ist recht ungenau. Aber ich experimentiere nur damit rum :-D

Code: Alles auswählen

#!/usr/bin/env python
#-*- coding: latin-1 -*-

import time

#
# Variablen
#
PRIMS = [9497,911]#,1567]#,136269,99961]#,1307,1567]
ID =    1234567890
IDmin = 1000000000
IDmax = 1999999999

#
# Encode
#
SCODE = []

for x in PRIMS:
    a = ID % x
    SCODE.append(a)
print SCODE

#
# Decode
#
st = time.time()

y = IDmin / PRIMS[0]
while y < (IDmax/PRIMS[0]):
    y += 1
    z = y * PRIMS[0] + SCODE[0]
    if z % PRIMS[1] == SCODE[1]:
        #if z % PRIMS[2] == SCODE[2]:
                print z
                #pass

print time.time()-st,"Sek\n"
SCODEa und SCODEb sind aus encode Ergebniss vom Python Script

Code: Alles auswählen

#include "stdio.h"
#include "time.h"

 main()
{
    int test = 0;
    int PRIMa = 9497;
    int PRIMb = 911;
    int ID = 1234567890; // not needed
    int IDmin = 1000000000;
    int IDmax = 1999999999;
    
    int SCODEa = 5375;
    int SCODEb = 732;
    int z = 0;
    
    int y = IDmin / PRIMa;
    while (y < (IDmax / PRIMb))
    {
        y += 1;
        z = y * PRIMa + SCODEa;
        if (z % PRIMa == SCODEb)
        {
            printf("Test: %i\n", z);
        }
    }
    return 0;
}
EDIT:
Achso Zeit stoppen im C Code wäre interesannt wenn das jemand umsetzen kann würd mich das freuen.
BlackJack

Eine Python-Version die bei mir ca. doppelt so schnell ist:

Code: Alles auswählen

import time

def test():
    # 
    # Variablen 
    # 
    prim_a = 9497
    prim_b = 911
    ID =    1234567890
    IDmin = 1000000000
    IDmax = 1999999999

    # 
    # Encode 
    # 
    scode_a = ID % prim_a
    scode_b = ID % prim_b
    
    # 
    # Decode 
    # 
    start = time.time()

    for y in xrange(IDmin // prim_a, IDmax // prim_a):
        z = y * prim_a + scode_a
        if z % prim_b == scode_b:
            print z 
    
    print time.time() - start, 's'

test()
Antworten