id/email
password
forgot password | create account
about | help | code help+videos | done | prefs
CodingBat code practice

 

orion.a.smith@gmail.com apcsa-encryption > apcsaEncryptCoprimes
prev  |  next  |  chance

Given two prime numbers prime1 and prime2, calculate and return in ascending order all the numbers that are coprime with (prime1 - 1) * (prime2 - 1), which are also less than that multiplication result. Two numbers are said to be coprime (or "relatively prime" or "mutually prime") if they have no common factors other than 1.


Why (prime1 - 1) * (prime2 - 1)?  Simply put, I could have made this task more focused by asking you to find the coprimes of a single int parameter.  But, I wanted you to see part of the role that prime numbers play in cryptography.  This exercise is a real problem that needs to be solved in real public-key algorithms like RSA, and is based on Euler's totient function.  To help you understand why you are doing this, see the excellent Khan Academy video at:
https://www.khanacademy.org/computing/computer-science/cryptography/modern-crypt/v/euler-s-totient-function-phi-function
Thus, if prime1 is 3 and prime2 is 7, (prime1 - 1) * (prime2 - 1) == (3-1)*(7-1) == 2*6 == 12. The list of numbers that is coprime with 12 is 1,5, 7 and 11. While in this example the list is composed of only prime numbers, it will often include many nonprime numbers as well. Preconditions: prime1 and prime2 are both prime, and prime1 != prime2.

This problem is based on, and helps to construct, the simplified public key encryption algorithm found at https://en.wikibooks.org/wiki/Cryptography/A_Basic_Public_Key_Example

These output lists get very big, very fast.  And the encryption algorithm should, fascinatingly enough, work with any of the available options.

Admittedly this is not a String problem at all but rather a pure math problem.  But trust me, we're getting to the actual encryption algorithm.

Java has a very useful class called BigInteger in the math package (must be referenced as java.math.BigInteger because CodingBat does not allow import statements), which even has a wonderful method designed explicitly for this sort of problem.  The constructor that you should use for BigInteger takes a String representation of the number as a parameter.  BigInteger can be converted back to int by using a method called intValue(), assuming that the number is small enough.
https://docs.oracle.com/javase/8/docs/api/java/math/BigInteger.html

apcsaEncryptCoprimes(3, 7) → [1, 5, 7, 11]
apcsaEncryptCoprimes(3, 13) → [1, 5, 7, 11, 13, 17, 19, 23]
apcsaEncryptCoprimes(5, 11) → [1, 3, 7, 9, 11, 13, 17, 19, 21, 23, 27, 29, 31, 33, 37, 39]

...Save, Compile, Run (ctrl-enter)

public ArrayList<Integer> apcsaEncryptCoprimes(int prime1, int prime2) { }

Editor font size %:
Shorter output


Forget It! -- delete my code for this problem

Progress graphs:
 Your progress graph for this problem
 Random user progress graph for this problem
 Random Epic Progress Graph

Java Help

Misc Code Practice

Difficulty: 240

Copyright Nick Parlante 2017 - privacy