Skip to main content
The National Cipher Challenge

Programming

A Tale of 2 Secrets Forums T.E.M.P.E.S.T. Programming

Tagged: 

Viewing 15 posts - 1 through 15 (of 57 total)
  • Author
    Posts
  • #93740
    Harry
    Keymaster

    Got any questions or suggestions about programming? You have a huge community of experts on this forum, so why not join them with some advice for newbies, or post your own questions here. No coursework help though!

    #102564
    Minthumbug
    Participant

    Not sure where I should post this, but it is a forum after all.

    Does anyone know whether the decrypted texts (other than the introduction) ever contain names? If they do, which ones might be used and do people have any UK name databases in the form of a txt file for my project?

    #102600
    upsidedown
    Participant

    The plaintexts often contain names, usually the names of the fairly small number of people involved in the current year’s story, and references to Harry and Jodie. There are databases of UK names around on the net, but if you are after a set of cribs, following along with the story in the weekly plaintexts should give you more useful ones than a generic list of names. It might be helpful to know what your usecase for these names is (e.g. for cribs this year, or some other fun project)?

    You can have a look at the plaintexts from previous years to get a general idea of the format.

    #107489
    ByteInBits
    Participant

    As this thread is about programming I will post some tasks.

    Write code to solve the following:

    A number is chosen at random from the first 100 positive integers.
    What is the percentage probability that the chosen number is not divisible by 3, 4, or 7?

    Post your code and show its output

    #107505
    ByteInBits
    Participant

    A nice easy one? maybe for the younger participants.
    I have posted under programming as you may want to do that and put up your code.

    Here is a mechanical puzzle I just made up (although the problem may not be original).
    Based on a 30 minute mechanical clock all geared up just for the fun of it!

    There are 30 markers evenly placed around the clock face
    There are 4 inner circles each with a stud that moves round the face
    The 4 studs are labled A B C and D, here is the starting and move data:

    Stud A is at marker 1 and moves 3 marks per minute
    Stud B is at marker 7 and moves 2 marks per minute
    Stud C is at marker 13 and moves 1 mark per minute
    Stud D is at marker 19 and moves 5 marks per minute

    Answer the following:
    1] At what numbered marker and after how many minutes will all the studs line up?
    2] If we let it run on, then after how many minutes will have passed when they line up again?
    3] Will the studs always all line up every 30 minutes? [YES/NO]
    4] Using the initial data is it posible for the studs to all line up on any other marker? [YES/NO]

    #107831
    T
    Participant

    I found this introduction to number theory:
    https://www.southampton.ac.uk/~wright/1001/Notes.pdf
    Chapter 9 has information on methods of attacking classic ciphers (most of the methods can be found on various websites so writing you own code would just be for fun).
    You don’t need to read the maths in the earlier chapters to write some code based on chapter 9, however the maths is interesting.

    #109792
    F6EXB_the_frenchy
    Participant

    @ByteInBits
    puzzle #107489

    import random
    
    i = input("Combien de boucles ? ")
    i = int(i)
    
    liste = [] # Liste des nombres non divisibles par 3, 4 ou 7
    
    for x in range(100): #0 à 99
    
        if (x+1)%3!=0 and (x+1)%4!=0 and (x+1)%7!=0:
            liste.append(x+1)
    print("\nListe des nombres non divisibles par 3, 4 ou 7 : \n",liste)
    
    compteur = 0
    for j in range(i):
        nombre = random.randint(1, 99)
    
        if nombre in liste:
            compteur += 1
    
    print("compteur = ", compteur)
    print("Pourcentage :", "{:.2%}".format(compteur/i))
    

    ===============================================================

    Combien de boucles ? 1000000

    Liste des nombres non divisibles par 3, 4 ou 7 :
    [1, 2, 5, 10, 11, 13, 17, 19, 22, 23, 25, 26, 29, 31, 34, 37, 38, 41, 43, 46, 47, 50, 53, 55, 58, 59, 61, 62, 65, 67, 71, 73, 74, 79, 82, 83, 85, 86, 89, 94, 95, 97]
    compteur = 423828
    Pourcentage : 42.38%

    #109842
    ByteInBits
    Participant

    @F6EXB_the_frenchy
    Mauvaise sortie !
    Essayez avec 100 boucles en entrée.

    #109850
    upsidedown
    Participant

    Re @ByteInBits #107489

    Here’s my solution based on intersections of sets of multiples of 3, 4 & 7 (like a Venn diagram).

    
    # python
    from functools import reduce
    
    def gcd(a, b): return a if b == 0 else gcd(b, a % b)
    def lcm(a, b): return a * b // gcd(a, b)
    def multiples(D, n): return n // reduce(lcm, D)
    
    n = 100
    m = lambda D: multiples(D, n)
    
    a, b, c = 3, 4, 7
    
    sum1 = m({a}) + m({b}) + m({c})
    sum2 = m({a, b}) + m({b, c}) + m({c, a})
    sum3 = m({a, b, c})
    
    total_div = sum1 - sum2 + sum3
    total_not_div = n - total_div
    
    print(f'{total_not_div / n:.0%}')
    

    42%

    #109859
    F6EXB_the_frenchy
    Participant

    Changing the random number generator:
    By replacing “import random” with “import secrets” in the first line, then replacing “number = random.randint(1, 99)” with
    “number = secrets.randbelow(100)” in line 16, the percentage is very close to the expected 42% after 1,000,000 or 10,000,000 loops.

    #109887
    ByteInBits
    Participant

    @F6EXB_the_frenchy
    Your first code ran ok for me when I entered 100 for loops (why do you need so many?)
    and gave the correct answer (an integer)

    I do not use Python much myself, PARI gp calculator is easier and usually needs less code

    Prematurely:
    I will put my code up now with its output – sorry if it spoils it for others but they can still offer
    their take on some code and can now check if it gives correct answer before posting

    \\ GP/PARI CALCULATOR Version 2.13.3
    c=0;for(i=1,100,if(i%3==0||i%4==0||i%7==0,c++;print1(” “i)));print(“\n”100-c”%”);
    3 4 6 7 8 9 12 14 15 16 18 20 21 24 27 28 30 32 33 35 36 39 40 42 44 45 48 49 51 52 54 56 57 60 63 64 66 68 69 70 72 75 76 77 78 80 81 84 87 88 90 91 92 93 96 98 99 100
    42%

    #109888
    ByteInBits
    Participant

    @upsidedown
    Nice interesting code and gives correct answer

    #109953
    ByteInBits
    Participant

    @F6EXB_the_frenchy
    Re: post #109887
    Sorry, I said it did but it did not give correct answer,
    I ran it 4 times with input of 100 loops
    it gave integers 47%, 45%, 43% and 41%

    Still not working with the alterations you gave 🙁
    Thing is you do not need to use random() numbers

    “err and err and err again but less and less and less” 😉
    “Dam computers, I made few mistakes in life before them, dam computers!” 😉
    “Good Judgment comes from Experience; Experience comes from Bad Judgment…” 😉

    #109955
    F6EXB_the_frenchy
    Participant

    @ByteInBits
    I was influenced by the sentence: “A number is chosen at random from the first 100 positive integers.” That’s why I did a random draw.
    With 100 loops, I sometimes get the theoretical number of 42%, but not every time I try. It’s like flipping a coin. I didn’t program a theoretical calculation (in fact, we can clearly see that among the first 100 integers, there are 42 numbers that are not divisible by 3, 4, or 7). I did a random draw, which results in deviations from the theoretical result. That’s why I used a large number of loops to get closer to the correct result.

    Translated with DeepL.com (free version)

    #109967
    ByteInBits
    Participant

    @F6EXB_the_frenchy

    No big deal, when I came across the question my first thoughts were like yours but it soon became a problem of how to code it.
    I love the fact that you had a go and was brave enough to post, bravo, some are afraid of being wrong.

    I am far far from being any expert on maths and or coding, reading others efforts is very rewarding, (thanks @upsidedown).

Viewing 15 posts - 1 through 15 (of 57 total)
  • You must be logged in to reply to this topic.
Report a problem