Skip to main content
The National Cipher Challenge

Programming

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

    Got any questions or suggestions about mathprogrammings? 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!

    #93952
    viaaa
    Participant

    Hiya,
    I was wondering if anyone had any recourses for learning to make cryptography programs in Python? I managed to make a caesar cipher a while back in 2021 or ’22, but gave up when I got to affine lol – I want to try again with more experience!

    #93985
    F6EXB_the_frenchy
    Participant

    Hello,
    You can buy or download:
    Cracking code with Python, by Al Sweigart.

    #94051
    viaaa
    Participant

    Thank you! I’ll look into Cracking Code with Python – I also found “A book on Classical Cryptography” by Madness last night, and that seems really helpful. If anyone in the future wants to read it, you can find it here, or you could find it on the BOSS training recourses.

    #96437
    ByteInBits
    Participant

    B Hi to all,
    y I am thinking of posting
    t some math questions and
    e will ask for your answer
    I along with your code used
    n for solving it.
    B You can use the language
    i that you are familiar with
    t I will give my answer and code
    s using PARI-gp Calculator.

    (was just trying out – hope the above displays ok)

    #96547
    ByteInBits
    Participant

    I am guessing this is the first in a series of coding challenges, so have at it! I think the setter is as interested in the code you write as in the arithmetical answer! I will hold off on publishing your solutions for a while so others can have a go. Harry

    Hopefully a reasonably easy one to kick off with.

    ******************************************************************** Question #1
    Find a number X that when divided by 4 leaves a remainder of 3 and
    when divided by 5 leaves a remainder of 1 and when divided by 7 leaves a
    remainder of 2 and when divided by 11 also leaves a remainder of 2 and
    when divided by 17 leaves a remainder of 12. (with X > 5,000 & x < 50,000)

    —————– same question put another way
    Find X
    when X is divided by 4,5,7,11,17 and in turn leaves the remainders 3,1,2,2,12
    (with X being greater than 5,000 but less than 50,000)

    For all questions: Give your code and the printout from it.

    #96964
    _madness_
    Participant

    Will this get past the censor?

    No! Did you think it would? Still pondering how best to release these. Think it would be best if replies on this thread came in two messages, one claiming to have made a solution, and which I can post without fear of spoilers, and a second with the solution in it that I can schedule for release when people have had a chance to try it. Then you can all crow, marvel or eat humble pie as appropriate. Fair? Harry

    #97023
    Astralica
    Participant

    As you wish Harry: I claim to have made a solution!

    Cool problem Bytes! I’m looking forwards to your next ones. How many will there be?

    #96986
    Rincewind
    Participant

    I have a solution, though not via any code!

    #98006
    ByteInBits
    Participant

    Hey coders, where are you?
    With 1,000’s of participants I thought there would be at least half a dozen
    replies to Question #1.

    If you are wandering. . .
    It does not matter how your code is done (what it looks like) as long as it
    produces the results needed, no one will say you could have done it in a better
    way, so don’t shy away from posting.

    When others post code we may all learn something from it.

    ================================================ QUESTION #2

    Write code in your chosen language and give answers to the following?

    Find a 4 digit positive integer N that has 60 divisors (includes 1 and N) and
    is also the sum of 42 consecutive primes. N and list the 42 primes.

    Show your code and its output giving the answer to the number and the primes.

    #96628
    AspiringPenguin
    Participant

    Solution for Q1
    Code (Python 3):

    for x in range(5001, 50000):
        if (x % 17) == 12:
            if (x % 11) == 2:
                if (x % 7) == 2:
                    if (x % 5) == 1:
                        if (x % 4) == 3:
                            print(x)

    Output:
    29031

    #98007
    ByteInBits
    Participant

    The heading of this thread is Programming so it is not all about math solving.
    On that basis here is

    ======================================================== Question #3
    WAYS WITH MONEY

    In how many ways can UK 1 pound (100 pence) be given as change using all the UK coins?
    UK Coins are: 1p,2p,5p,10p,20P,50p,£1(100p) and £2(200p), we disregard the £2 coin here.

    HERE ARE THE THE FIRST FIVE AND LAST FIVE SETS THAT MY CODE GAVE
    I have edit replaced some values with a ? (else it would give the answer away)

    \\ ===================== PROGRAM OUTPUT ======================
    \\ set#) coin count of(coin value)
    \\ 1) 0(1p) 0(2p) 0(5p) 0(10p) 0(20p) 0(50p) 1(100p)
    \\ 2) 0(1p) 0(2p) 0(5p) 0(10p) 0(20p) 2(50p) 0(100p)
    \\ 3) 0(1p) 0(2p) 0(5p) 3(10p) 4(20p) 1(50p) 0(100p)
    \\ 4) 0(1p) 0(2p) 0(5p) 4(10p) 2(20p) 1(50p) 0(100p)
    \\ 5) 0(1p) 0(2p) 0(5p) 5(10p) 0(20p) 1(50p) 0(100p)
    \\ ?) 95(1p) 0(2p) 0(5p) 0(10p) 1(20p) 0(50p) 0(100p)
    \\ ?) 95(1p) 0(2p) 1(5p) 0(10p) 0(20p) 0(50p) 0(100p)
    \\ ?) 96(1p) 2(2p) 0(5p) 0(10p) 0(20p) 0(50p) 0(100p)
    \\ ?) 98(1p) 1(2p) 0(5p) 0(10p) 0(20p) 0(50p) 0(100p)
    \\ ?) 100(1p) 0(2p) 0(5p) 0(10p) 0(20p) 0(50p) 0(100p)

    \\ The number of possible ways (sets) is: ?

    ================= TASK

    Write Code (function) and have it display the first and last TEN sets similar to the above.

    SHOW YOUR CODE AND ITS OUTPUT

    #98019
    ByteInBits
    Participant

    *ALERT*
    Ho my bad! Just noticed – It looks like some of the lines are incorrect.

    Hope you can do better than me and give correct output and number of sets.

    Now I’VE got to find what I have done wrong!!!

    #98022
    Astralica
    Participant

    Question 2:

    Solution: 5040
    Sum of 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229.

    I just checked all the 4-digit sums of 42 consecutive primes.

    
    import math
    
    def generate_primes(n): # Sieve of Eratosthenes
      nums = [2] + [i for i in range(3, n + 1, 2)]
      _primes = set()
      
      for i in range(3, int(n**0.5)):
        for j in range(i**2, n + 1, i):
          _primes.add(j)
      
      return list(filter(lambda x: x not in _primes, nums))
    
    def primeSlicer(): # Finds all sums of 42 consecutive primes
        primes = generate_primes(355) # Stops at the last 4-digit num
        for i in range(len(primes) - 41):
            yield sum(primes[i: i+42])
            
    def divisorCounter(num):
        counter = 0
        for i in range(1, int(num**0.5) + 1):
            if (num % i == 0): # Sorts out square numbers here.
                counter += 1 if i == num // i else 2
    
        return counter
        
    for v in primeSlicer(): # Checks each prime number for the number of divisors
        if divisorCounter(v) == 60:
            print(v)
    
    
    #98023
    Astralica
    Participant

    Solution for Question 3:
    Here are the first, and last 10 sets, as requested.

    1p: 0 | 2p: 0 | 5p: 0 | 10p: 0 | 20p: 0 | 50p: 0 | 100p: 1
    1p: 0 | 2p: 0 | 5p: 0 | 10p: 0 | 20p: 0 | 50p: 2 | 100p: 0
    1p: 0 | 2p: 0 | 5p: 0 | 10p: 0 | 20p: 5 | 50p: 0 | 100p: 0
    1p: 0 | 2p: 0 | 5p: 0 | 10p: 1 | 20p: 2 | 50p: 1 | 100p: 0
    1p: 0 | 2p: 0 | 5p: 0 | 10p: 2 | 20p: 4 | 50p: 0 | 100p: 0
    1p: 0 | 2p: 0 | 5p: 0 | 10p: 3 | 20p: 1 | 50p: 1 | 100p: 0
    1p: 0 | 2p: 0 | 5p: 0 | 10p: 4 | 20p: 3 | 50p: 0 | 100p: 0
    1p: 0 | 2p: 0 | 5p: 0 | 10p: 5 | 20p: 0 | 50p: 1 | 100p: 0
    1p: 0 | 2p: 0 | 5p: 0 | 10p: 6 | 20p: 2 | 50p: 0 | 100p: 0
    1p: 0 | 2p: 0 | 5p: 0 | 10p: 8 | 20p: 1 | 50p: 0 | 100p: 0

    1p: 90 | 2p: 0 | 5p: 2 | 10p: 0 | 20p: 0 | 50p: 0 | 100p: 0
    1p: 90 | 2p: 5 | 5p: 0 | 10p: 0 | 20p: 0 | 50p: 0 | 100p: 0
    1p: 91 | 2p: 2 | 5p: 1 | 10p: 0 | 20p: 0 | 50p: 0 | 100p: 0
    1p: 92 | 2p: 4 | 5p: 0 | 10p: 0 | 20p: 0 | 50p: 0 | 100p: 0
    1p: 93 | 2p: 1 | 5p: 1 | 10p: 0 | 20p: 0 | 50p: 0 | 100p: 0
    1p: 94 | 2p: 3 | 5p: 0 | 10p: 0 | 20p: 0 | 50p: 0 | 100p: 0
    1p: 95 | 2p: 0 | 5p: 1 | 10p: 0 | 20p: 0 | 50p: 0 | 100p: 0
    1p: 96 | 2p: 2 | 5p: 0 | 10p: 0 | 20p: 0 | 50p: 0 | 100p: 0
    1p: 98 | 2p: 1 | 5p: 0 | 10p: 0 | 20p: 0 | 50p: 0 | 100p: 0
    1p: 100 | 2p: 0 | 5p: 0 | 10p: 0 | 20p: 0 | 50p: 0 | 100p: 0

    Surprisingly, I could just bruteforce this question without any optimisations.

    Initially, I was gonna make a recursive solution that would prune branches if the sum exceeded 100, but it looks like that wasn’t necessary.

    
    print("1p: 0 | 2p: 0 | 5p: 0 | 10p: 0 | 20p: 0 | 50p: 0 | 100p: 1")
    for i in range(101): # 1p coins
        for j in range(51): # 2p coins
            for k in range(21): # 5p coins
                for m in range(11): # 10p coins
                    for n in range(6): # 20p coins
                        for o in range(3): # 50p coins
                            if sum([i, 2*j, 5*k, 10*m, 20*n, 50*o]) == 100:
                                print("1p:", i, "| 2p:", j, "| 5p:", k, "| 10p:", m, "| 20p:", n, "| 50p:", o, "| 100p: 0")
    
Viewing 15 posts - 1 through 15 (of 50 total)
  • You must be logged in to reply to this topic.
Report a problem