Hot answers tagged

20 votes

What's the point of the modulo operation in the Caesar cipher formula?

Let's consider the encryption formula given in the problem set: cᵢ = (pᵢ + k) % 26 Here, as in the problem set,cᵢ represents the value of the encrypted or enciphered letter, pᵢ represents the value ...
user avatar
5 votes
Accepted

Modulo not working in caesar

Modulo is working correctly, but you've overlooked something. The idea behind using modulo is this. When you represent letters as numbers between 0 and 25 inclusive, and add a number to shift the ...
user avatar
  • 63.4k
3 votes

why do we use remainder 26 (%26)?

% 26 will always give you a number between 0 and 25. The Caesar algorithm works on the assumption that your letters A-Z (or a-z) are represented by the numbers 0-25, so if you have 'z' (25) and a key ...
user avatar
  • 18.3k
3 votes

What's the point of the modulo operation in the Caesar cipher formula?

The purpose of the formula is to calculate the new (cyphered) value for a given character on a string, considering a swift value. The formula has 4 components: ci = ciphered character pi = current ...
user avatar
3 votes
Accepted

Modulo not working in Greedy

If you tried typecasting to int, still the error would have existed because you are still storing the value in a float. So its basically like, original value = 12.27 round()'s output = 1227.00 ...
user avatar
  • 3,316
3 votes

Modulo not working in Greedy

Modulo Operator ( % ) requires both the operands to be of type int . In your code change is a floating point variable. Use an integer variable to store the rounded value.
user avatar
2 votes
Accepted

modulo returning wrong value in Greedy

That's how the remainder operator just works. You may think of it that way: given a = 3 and b = 2, the expression a % b // results in 1 because a 3 can have a maximum of a single (i.e., one) 2 in ...
user avatar
  • 17.4k
2 votes

How to wrap around the jth element in vigenere cipher efficiently?

I am among those people who suggest you to use 2 loop variables, actually its 1. Use a variable (say i) to iterate over each element of the input string and maintain another variable (say j) to hover ...
user avatar
  • 3,316
2 votes
Accepted

pset2 Vigenere Modulo

In order to use modulo to wrap around the dictionary you first need to offset both key and plaintext to convert them to values between 0 and 25, and then add them and use modulo. That part is just as ...
user avatar
  • 1,677
2 votes
Accepted

Typecasting with Modulo in Greedy

Modulo only works with integers. You are attempting to apply it to a float. If this answers your question, please click on the check to accept. Let's keep up on forum housekeeping. ;-)
user avatar
  • 63.4k
1 vote
Accepted

cash CS50 2020 pset1 modulo

Why are you using modulo for this? It produces an unreliable result in this usage. Further, the actual numbers don't make sense. The modulo function returns the remainder after an integer division. ...
user avatar
  • 63.4k
1 vote

Why is d = 185 to satisfy de = 1 mod m (where m = 924)? (Wk2, shorts: cryptography)

de = 1 (mod m) where e is equal to 5 and m is equal to 924 means (5 * d) % 924 = 1 Thomas later in the video used the extended Euclidean algorithm to find a value of d that would satisfy the ...
user avatar
  • 17.4k
1 vote
Accepted

PSET1 - Credit Checksum Problem

test.c:35:49: error: invalid operands to binary expression ('double' and 'double') printf("%lld\n", ((l / (pow(10.00, i))) % 10)); ~~~~~~~~~~~~~~~~~~~~~ ^ ~~ 1 error ...
user avatar
  • 63.4k
1 vote
Accepted

PSet1 cash, modulo problem

Welcome to the world of (binary) floating point. A value like 4.2 cannot be precisely represented in binary, it would be represented by an infinite sequence of binary digits. Obviously we don't have ...
user avatar
  • 20.7k
1 vote
Accepted

Pset1 Cash Understanding modulo operator and while loops

You have created an infinite loop. If the loop starts, it will never exit the loop. It tests the value contained in amount to decide to continue or exit, but since the value of amount is not changed ...
user avatar
  • 63.4k
1 vote
Accepted

Pset 2 Caesar: "invalid operands to binary expression ('string' (aka 'char *') and 'int')"

In that formula, use p[i], the ith character of p, not p itself.
user avatar
  • 20.7k
1 vote
Accepted

Another modulo question for CS50 Caesar

I would suggest that you convert both your uppercase and lowercase chars into an index value of 0-25. That is the premise of the caesar formula.
user avatar
  • 18.3k
1 vote
Accepted

Trying to make cash.c from pset 1 without mod

Please have another look at the second hint of this exercise (https://docs.cs50.net/2018/x/psets/1/cash/cash.html#hints). After you have followed that advise, you will be able to use the modulo ...
user avatar
1 vote
Accepted

Output of characters at specific place place using uint_list

If you want to print something different based on a condition, you need to use a conditional like an if. As written this program will only ever output (sign) and 5 dashes. Full stop. This does not ...
user avatar
1 vote
Accepted

PSET1 / Credit / Modulo problem / Error: invalid operands to binary expression ('double' and 'double')

the definition of the pow function says that it returns a double that is the reason of your error, since the operator modulo requires integers, it is possible that it works if we do a casting although ...
user avatar
  • 5,172
1 vote

Refactoring Pset1 Greedy Modulo

You could simply use coins as the variable that holds the coin value and add to it with each equation. coins = 0; coins += amount/25; ... coins += amount/10 .... etc etc. Then, at the end, coins ...
user avatar
  • 18.3k
1 vote
Accepted

vigenere key wraparound issue

Take a close look at this line and think very carefully about what the modulo operation is being applied to: int cipherKey = (((key[i]) - 'A') % KeyLen); It coverts a key letter to a number between ...
user avatar
  • 63.4k
1 vote
Accepted

greedy with modulo

You're creating multiple shadow variables in the code. A shadow variable is a new var with the same name as one that has been created earlier, but the new var is created in a place where it will have ...
user avatar
  • 63.4k
1 vote
Accepted

strange output in vigenere. Even when done by hand to add ascii values

We have 26 characters in English alphabet, so use %26 and not %25. Also, note what you apply that %26 to, you seem to apply it to tolower(argv[1][counter]) - 97 only instead of to the whole sum.
user avatar
  • 20.7k
1 vote
Accepted

Pset2 Vigenere z key stops functionning after e

By the let me tell you that your program is not working at all It's showing error something like this b.c:16:10: runtime error: null pointer passed as argument 1, which is declared to never be ...
user avatar
  • 264
1 vote

pset2 Vigenere Modulo

You were correct. You must use the modulus operator to ensure that the text output is between A and Z. However, you are wrapping the key, so that it is from A to Z. You must wrap the output. So ...
user avatar
  • 107
1 vote

How to wrap around the jth element in vigenere cipher efficiently?

another way to do it j++; j = j % v; where j is the jth char of key and v is strlen of key. when placed at the end of your shift functions within your if statement, j will increment only when you've ...
user avatar
1 vote

how do I restrict cipher to alpha only - I am getting symbols that come after "z"

A few errors to point out: your code is difficult to read because it has not been indented properly. I believe your formula for shifting the characters is wrong. Lets say we take 'z' which is 122 on ...
user avatar
  • 171
1 vote

How to calculate modulo?

Lets find pow(x,y)%n Wikipedia lists this property here. Now, since we know how to find the modulus of product of 2 big numbers, then we can use this identity in the following way to do it for ...
user avatar
  • 3,316
1 vote

How to calculate modulo?

After watching the short on RSA, I guess you didn't mean the XOR bitwise operator by the symbol (^), but rather, you meant exponent. The powl() function (declared in math.h) can be useful for that ...
user avatar
  • 17.4k

Only top scored, non community-wiki answers of a minimum length are eligible