views:

125

answers:

2

I just started university and managed to spend my entire first week sick, so I'm having a hard time figuring this out. I'm supposed to crack the caesar cypher. I have to declare a function crack : int * int -> int so that if (k, c) are of the type int, where k is the decrypted text and c the encrypted text, calling crack(k, c) will return the key (mod 10) n, which is needed to get c to k. An example would be that calling crack(20458790, 64892134) would return 4.

If c isn't a correctly encoding of k, the function doesn't have to actually work.

I hope I'm being clear enough here. I understand the actual assignment here (I have k and c, I need n), but I don't know how to show it in my code.

Thanks!

+1  A: 

You didn't specify what should happen when the second number isn't a caesar encoding of the first, so I'm going to assume it does not matter.

So in order to get, you just need to take any digit (most conveniently the last) from the first number and subtract that from the digit at the same position of the second number.

In other words, you can just do: (c mod 10 - k mod 10) mod 10

sepp2k
I forgot to add that, sorry. If the second number (c) isn't a proper encoding of the first, the function doesn't actually have to work. I tried what you suggested, but for some reason it's returning 6 instead of 4 when I input the numbers from above. Any idea why it would do this? Thanks!
GeorgeWChubby
Thank you so much! I think I finally got it. I just had to say (c mod 10 - k mod 10) mod 10 instead. I'm not sure why, but it works.
GeorgeWChubby
@George: Oops, yes, I switched up the variable names. It works because it's simply subtracting the last digit of k from the last digit of c. So if you add that difference to the last digit of c, you get the last digit of k. And the same goes for every other digit because all digits have the same difference.
sepp2k
Thanks. Now I get it :)
GeorgeWChubby
A: 

Heii guys, I have the same problem, but i don't get it?.. anyone? GeorgeWChubby, how did you do it?

thanks

peter81
I just did exactly what sepp2k said. Just write this: fun crack(k, c) = (c mod 10 - k mod 10) mod 10; and it should work. It does for me.
GeorgeWChubby
yes but what do I do with n?...and how do I use to show (20458790, 64892134) would return 4?
peter81
So, after you write fun crack (k, c) = (c mod 10 - k mod 10) mod 10; and hit enter, you should see this: val crack = fn : int * int -> int. Enter this: crack(20458790, 64892134) and you should see this: val it = 4 : int. So now you can see that if you run 20458790 and 64892134 as k and c, you'll get the output 4 which is n. See what I mean?
GeorgeWChubby
oh, yes, that's also how I did it.. and yes I get 4 as n..But sorry I stil don't really get how it works. Because after that, I have to test them in my asigment.. have you mad "tests"?...
peter81
I'm not sure what you're supposed to do. What do you mean "test" them?
GeorgeWChubby
okay let me explain then:
peter81
or you know what, i get it know.. i thnik :P, thanks so much..I hope I can ask you again, if I got more problems :)
peter81
Sure, but I'll warn you: I'm no expert. I'm wondering, where are you from?
GeorgeWChubby
hehe.. okay, neither am I ;), I'm from Denmark, and you?
peter81
So am I. You from DIKU?
GeorgeWChubby
yes I am :D haha.. nice...
peter81
It's a small world :P
GeorgeWChubby
yes It is.. ;DI have made the "groupeopgave" ..but in the assignment 1i1/1i2/1i3... you have to make tests..
peter81
For 1i2 I just ran the function crack(72926349, 27471894) and checked the result against the function from the group assignment. Then I used the "vejledning i afprøvning" to see how to write it. I'm still working on 1i3, I haven't quite figured out how to do that.
GeorgeWChubby
I'm still pretty lost, and don't really get it :/ - I have read alot, but I don't know how to make it up.
peter81
I wish I could help you, but I'm stuck myself.
GeorgeWChubby
yes i know, but tell me if you get any solutions for them... like I said, I'm lost! :)
peter81
dude......... ?
peter81
Sorry, I was trying to get 1i3 working - still not there though. I've only been able to prove that the function works with (72926349, 27471894), but that's it.
GeorgeWChubby
yes me too! - I can't understand 1i3! :/
peter81
wanna share, so I send my copy to you, and you send to me?.. then we can look, what we've made wrong maybe?
peter81
dude, are you sleeping!? :P eheh
peter81
Sorry man, had guests come over and completely forgot about the assignment.
GeorgeWChubby