Is there a cleaner way to find p*q = n, when p and q are coprime?
Basically trying to write a simple RSA brute force. I have modulus N and I'm trying to find p*q=n where p and q are coprimes.
So far i'm able to find the prime factors of N and I can put them in a list. Then taking that list apart into variables then going through each pair to find coprimes. It's messy, and was wondering if there was a cleaner way of doing it?
my code so far:
from fractions import gcd
import sys
n= 17
print "Modulus (n) = ", n
print("p & q are:")
# While Loop to find prime factors
i=1
list=[]
while(i<=n):
k=0
if(n%i==0):
j=1
while(j<=i):
if(i%j==0):
k=k+1
j=j+1
if(k==2):
list.append(i)
i=i+1
# takes list of prime factors and splits to variables
for n, val in enumerate(list):
globals()["var%d"%n] = val
# really messy, tries to multiply first 2 variables, if coprime, print the variables
# if no coprime factors move on to next 2 variables
# if no prime factors in the first place, exit
try:
gg0 = gcd(var0,var1)
except:
print "No Prime Factors"
sys.exit()
try:
gg1 = gcd(var1,var2)
if gg1 == True:
print var1, var2
except:
if gg0 == True:
print var0, var1
See also questions close to this topic

Error when nonEnglish characters present
When the user inserts nonEnglish characters, the script stops with an error.
# * coding: utf8 * from Tkinter import * wnd = Tk() name_A = Entry(wnd) name_A.pack() def AAAA1(): A1 = name_A.get() with open("file_name.TXT", 'w') as f: f.write(A1) Button(wnd, text="GO", command=AAAA1).pack() wnd.mainloop()
UnicodeDecodeError: 'ascii' codec can't decode byte 0xd7 in position 0: ordinal not in range(128

When I run this code, it gives None instead of the 'Python' getting appended to the List. Why is that so?
When I run this code, it gives None instead of the 'Python' getting appended to the List. Why is that so?
input_tuple = ('Monty Python', 'British', 1969) input_list = list(input_tuple) print(input_list) input_list_1 = input_list.append('Python') print(input_list_1)

Python (higher order function)  Don't understand how to approach this, any advice or hints please
def combine(f, op ,n): result = f(0) for i in range(n): result = op(result, f(i)) return result def smiley_sum(t): def f(x): ... def op(x, y): ... n = ... # Do not modify this return statement return combine(f, op, n)
The question asks to define the two functions in terms of combine. However, I do not exactly understand what that means and how to implement that. From how I see it, the 'combine' function is the main function and we have to write the other two functions from scratch (without utilising combine in them).

How to get all file list in my storage?
I'm making application that find specific file in my storage. So I put all file list into List.
Root = Environment.getExternalStorageDirectory().getAbsolutePath(); List<String> fileList = new ArrayList<String>(); searchFile(new File(Root)); void searchFile(File directory){ File[] files = directory.listFiles(); try{ if(directory.exists()) { File[] files = directory.listFiles(); for (int i = 0; i < files.length; i++) { if(files[i].exists()) { if (files[i].isDirectory()) { File[] file = files[i].listFiles(); for (int j = 0; j < file.length; j++) { searchFile(file[j].getPath()); } } else fileList.add(files[i].getPath()); } } } } catch(Exception ex){} }
But My the number of all file is more than 60000. So When I tried to debug, It worked so slowly.
How can I get All file list in my storage quickly?

Python: How do I print a tuple using try and except?
Python: Im checking to see if a list has all digits, if yes, print them as tuple(a), if not, print an Error message. My code is printing both the error and the tuple(a) if the list contains letters, how can i just print the error message without the tuple?
#list_to_tuple function goes here def list_to_tuple(a_list): a = [] for i in a_list: try: i = int(i) a.append(i) except: print("Error. Please enter only integers.") print(tuple(a)) def main(): a_list = input("Enter elements of list separated by commas:").strip().split(',') list_to_tuple(a_list) main()

Is there a simpler way to set multiple variables from a list in python?
I am attempting to set variables all at once from a list...
This is what I have so far:
firstName, lastName, age, email, pin, balance = row[0], row[1], row[2], row[3], row[4], row[5]
Is there a simpler way to do this? Something like:
firstName, lastName, age, email, pin, balance = row[0, 1, 2, 3, 4, 5]
I know that won't work, but I'm just trying to figure out if something like that is possible :)

Replicating onion address algorithim?
By all sources I can find, onion / hidden service "domains" are generated by this process:
Generate a 1024bit RSA keypair Take the SHA1 of the public key Base32 encode the first 80 bytes of the hash
I have tried to replicate it, by first generating a memorable vanity URL with Scallion, "zzzzzzycizaamf47.onion". I then wrote a PHP script to derive the public key from that private key, strip the formatting, hash it, truncate to 10 chars, and base32 encode it. First of all I found three different "encode as base32" functions, all which give different results. This is the code, with results commented after the three functions:
$privKey = 'MIIEpAIBAAKC ~snip~ azScNv5A'; $rsa = new Crypt_RSA(); $rsa>loadKey($privKey); $pubKey = $rsa>getPublicKey(CRYPT_RSA_PUBLIC_FORMAT_PKCS1); $pubKey = str_replace(array("\r", "\n", ' ', '=', 'BEGINRSAPUBLICKEY', 'ENDRSAPUBLICKEY'), '', $pubKey); // $pubKey = sha1($pubKey); $pubKey = substr($pubKey, 0, 10); echo base_convert($pubKey, 16, 32).PHP_EOL; //Result: 68s3ii2 echo crockford32_encode($pubKey).PHP_EOL; //Result: 60rkjcb365hp2d1j $base32 = new Base2n(5, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567', FALSE, TRUE, TRUE); echo $base32>encode($pubKey).PHP_EOL; //Result: GAYTSMLDGFRWCNBS
What's going wrong here? Why do the three "base32" functions give different results? Here is the code for crockford32_encode, and here is the code for Base2n. I have tried nearly all combinations of formatting / whitespace stripping and character truncation, and have never gotten close to the desired "zzzzzzycizaamf47".

How to decrypt an AES GCM encrypted image?
This is an example of such image ex.

Is RSA encryption supported in Groovy?
I want to encrypt a string through RSA in groovy language. But I am not sure whether its possible or not.Please help.

Math in Multiple fields
Having trouble doing multiplication, division and addition in multiple fields that are populated by picker view selections. All of my picker views display the data but when I click the button to calculate, nothing happens then it errors out on the calculate function.
class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate { @IBOutlet weak var textColor: UITextField! @IBOutlet weak var textRatio: UITextField! @IBOutlet weak var textDensity: UITextField! @IBOutlet weak var textRnumber: UITextField! @IBOutlet weak var textWeight: UITextField! @IBOutlet weak var textCdensity: UITextField! @IBOutlet weak var textCatalyst: UITextField! @IBAction func Calculate(_ sender: UIButton) { let a: Int? = Int(textColor.text!) let b: Int? = Int(textWeight.text!) let c: Int? = Int(textRnumber.text!) let d: Int? = Int(textCdensity.text!) let volume = b! / a! let total = volume * d! let catalyst = total * c! let catalysttext = String(catalyst) textCatalyst.text = catalysttext } var colorBase = ["E100 Blackout","E110 Midnight","E120 Smoke", "E130 Earth", "E140 Jungle", "E150 Sand", "E160 Concrete", "E170 Coyote M17", "E180 Daniel Defense Deep Woods","E190 20150", "H109 Gloss Black", "H112 Cobalt", "H115 Sig Teal", "H122 Gold", "H125 ICON Grey", "H126 Dewalt Yellow", "H127 Kel Tec Blue", "H128 Hunter Orange", "H130 Combat Grey", "H131 Dark Green", "H133 Cross Canyon Arms Green", "H135 Ratio Gold", "H136 Snow White", "H137 Gloss White", "H139 Steel Grey", "H140 Bright White", "H141 Prison Pink", "H142 Light Sand", "H143 Benelli Sand", "H144 Corvette Yellow", "H145 Great White", "H146 Graphite Black", "H147 Satin Mag", "H148 Burnt Bronze", "H149 Copper Brown", "H150 Savage Stainless", "H151 Satin Aluminum", "H151E Stain Aluminum Electrostatic", "H152 Stainless", "H153 Shimmer Gold", "H154 Medium Brown", "H155 Taurus Stainless", "H157 Bright Nickel", "H158 Shimmer Aluminum", "H160 Colt Coyote", "H166 Electric Yellow", "H167 USMC Red", "H168 Zombie Green", "H169 Sky Blue", "H170 Titanium", "H171 NRA Blue", "H172 Sea Blue", "H175 Robin's Egg Blue", "H184 Glock Grey", "H185 Blue Titanium", "H188 Magpul Stealth Grey", "H189 Noveske Bazooka Green", "H190 Armor Black", "H196 A.I. Sage Green", "H197 Wild Purple", "H198 A.I. Pale Brown", "H199 Desert Sand", "H200 Highland Green", "H201 McMillan Grey", "H20180 FS Tan", "H202 McMillan Olive", "H203 McMillan Tan", "H204 Multicam Green", "H205 APA Sage", "H206 Tarjac Green", "H207 Wild Green", "H208 Wild Pink", "H210 Sig Dark Grey", "H211 BAE Green", "H212 Federal Brown", "H213 Battleship Grey", "H214 Smith's Grey", "H215 Smith's Brown", "H216 Smith and Wesson Red", "H217 Bright Purple", "H218 School Bus Yellow", "H219 Gun Metal Grey", "H220 Ridgeway Blue", "H221 Crimson", "H222 Peach Melba", "H223 Rode Pink", "H224 Sig Pink", "H225 Mud Brown", "H226 Patriot Brown", "H227 Tactical Grey", "H229 Sniper Green", "H231 Magpul Foliage Green", "H232 Magpul O.D. Green", "H233 Benchmade O.D. Green", "H234 Sniper Grey", "H235 Coyote Tan", "H236 O.D. Green", "H237 Tungsten", "H238 Midnight Blue", "H239 Benchmade Coyote Tan", "H240 Mil Spec O.D. Green", "H241 GA O.D. Green", "H242 Hidden White", "H243 Safety Orange", "H244 Bright Pink", "H245 SOCOM Blue", "H246 Desert Gold", "H247 Desert Sage", "H248 Forest Green", "H249 Nut Brown", "H250 A.I. Dark Earth", "H251 Green Beret Green", "H252 Midnight Green", "H254 Muddy River", "H255 Crushed Silver", "H256 Desert Verde", "H257 Texas Tan", "H258 Chocolate Brown", "H259 Barrett Bronze", "H260 Standard ACU Green", "H261 Austrian Flat Dark Earth", "H262 Stone Grey", "H263 Foliage Green", "H264 Mil Spec Green", "H265 Flat Dark Earth", "H266 RedOrange", "H267 Magpul Flat Dark Earth", "H268 Troy Coyote Tan", "H269 Barrett Brown", "H270 Daniel Defense Mil Spec+", "H271 Daniel Defense Tornado", "H272 Daniel Defense Deep Woods", "H280 MultiRegional Earth", "H281 Woodland Tan", "H294 Midnight Bronze", "H295 Cobalt Kinetics Slate", "H296 Cobalt Kinetics Green", "H300 Armor Clear", "H301 Matte Armor Clear", "H30118 FS Field Drab", "H302 Pearl Clear", "H303 RotoBlue Clear", "H30372 FS Brown Sand", "H30372 FS Brown Sand(Big Blue)", "H33446 FS Sabre Sand", "H36357 FS Grey", "H400 Jesse James Eastern Front Green", "H401 Jessie James Civil Defense Blue", "H402 Jesse James Cold War Grey", "H7504M Matte Brown", "H8000 RAL 8000", "H900 Electrical Barrier"] var outBase = ["1.33", "1.38", "1.38", "1.42", "1.40", "1.42", "1.38", "1.42", "1.40", "1.40", "1.34", "1.47", "1.10", "1.43", "1.41", "1.52", "1.39", "1.33", "1.41", "1.34", "1.36", "1.38", "1.43", "1.43", "1.41", "1.39", "1.39", "1.35", "1.36", "1.09", "1.52", "1.43", "1.38", "1.36", "1.36", "1.38", "1.36", "1.10", "1.40", "1.38", "1.40", "1.35", "1.12", "1.36", "1.36", "1.04", "1.37", "1.46", "1.42", "1.38", "1.39", "1.05", "1.48", "1.39", "1.43", "1.39", "1.36", "1.37", "1.43", "1.41", "1.38", "1.36", "1.38", "1.37", "1.36", "1.38", "1.37", "1.40", "1.40", "1.38", "1.05", "1.39", "1.37", "1.37", "1.35", "1.35", "1.40", "1.32", "1.36", "1.33", "1.36", "1.35", "1.36", "1.49", "1.40", "1.38", "1.41", "1.42", "1.37", "1.40", "1.36", "1.37", "1.37", "1.36", "1.37", "1.38", "1.36", "1.42", "1.39", "1.34", "1.37", "1.30", "1.38", "1.32", "1.38", "1.39", "1.35", "1.36", "1.37", "1.34", "1.39", "1.39", "1.38", "1.33", "1.31", "1.35", "1.35", "1.36", "1.38", "1.39", "1.38", "1.37", "1.44", "1.36", "1.39", "1.30", "1.37", "1.33", "1.35", "1.35", "1.48", "1.36", "1.45", "1.38", "1.38", "1.37", "1.38", "0.93", "0.92", "1.40", "0.93", "0.92", "1.36", "1.36", "1.40", "1.36", "1.37", "1.05", "1.04", "1.37", "1.40", "1.48"] var ratioBase = ["HClear", "12:1", "13:1", "14:1", "15:1", "16:1", "17:1", "18:1", "19:1", "20:1", "21:1", "22:1", "23:1", "24:1"] var rnumberBase = ["1/9", "1/12", "1/13", "1/14", "1/15", "1/16", "1/17", "1/18", "1/19", "1/20", "1/21", "1/22", "1/23", "1/24"] var cdensityBase = ["1.05", "1.07", "1.07", "1.07", "1.07", "1.07", "1.07", "1.07", "1.07", "1.07", "1.07", "1.07", "1.07", "1.07"] var picker = UIPickerView() var picker2 = UIPickerView() override func viewDidLoad() { super.viewDidLoad() picker.delegate = self picker.dataSource = self textColor.inputView = picker picker2.delegate = self picker2.dataSource = self textRatio.inputView = picker2 textCdensity.inputView = picker2 self.picker.selectRow(32, inComponent: 0, animated: false) self.picker2.selectRow(7, inComponent: 0, animated: true) let toolBar = UIToolbar() toolBar.sizeToFit() let doneButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.done, target: self, action: #selector(doneClicked)) toolBar.setItems([doneButton], animated: false) textColor.inputAccessoryView = toolBar textRatio.inputAccessoryView = toolBar textWeight.inputAccessoryView = toolBar } @objc func doneClicked() { view.endEditing(true) } func numberOfComponents(in pickerView: UIPickerView) > Int { return 1 } func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) > Int { if pickerView == picker{ return colorBase.count }else if pickerView == picker2{ return ratioBase.count }else if pickerView == picker2{ return cdensityBase.count } return 1 } func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) { if pickerView == picker{ textDensity.text = outBase[row] }; if pickerView == picker{ textColor.text = colorBase[row]}; if pickerView == picker2{ textRnumber.text = rnumberBase[row] }; if pickerView == picker2{ textRatio.text = ratioBase[row]}; if pickerView == picker2{ textCdensity.text = cdensityBase[row] } } func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) > String? { if pickerView == picker{ return colorBase[row] }else if pickerView == picker2{ return ratioBase[row] }else if pickerView == picker2{ return cdensityBase[row] } return "" }

Multiplying Variables Together
So, I know that this question has probably been answered, but I couldn't find anything. So, keeping in mind I don't know anything in terms of the stuff most people on this site would normally mention nonchalantly, how do you multiply variables?
>>> print("This is a basic multiplication calculator, useable for two numbers and only two.") This is a basic multiplication calculator, useable for two numbers and only two. >>> num1 = input("Num1, please.") Num1, please. >>> num2 = input("Num2, please.") Num2, please. >>> numfin = num1 * num2 Traceback (most recent call last): File "<pyshell#3>", line 1, in <module> numfin = num1 * num2 TypeError: can't multiply sequence by nonint of type 'str'
That's my code basic, obviously, but I don't know what went wrong.

Issue with Multiplication Table
I'm looking to see if anyone can help me figure out what I've got missing here in my code:
def Table(n): for x in range(1,n): for y in range(1,n): z = (x*y) print(z+1, end="\t") print()
I'm using Spyder using Python 3.6. My current output is as such
Table(10)
:2 3 4 5 6 7 8 9 10 3 5 7 9 11 13 15 17 19 4 7 10 13 16 19 22 25 28 5 9 13 17 21 25 29 33 37 6 11 16 21 26 31 36 41 46 7 13 19 25 31 37 43 49 55 8 15 22 29 36 43 50 57 64 9 17 25 33 41 49 57 65 73 10 19 28 37 46 55 64 73 82
As you can see I'm missing the column for
1
when I call the variable & put inTable(10)
. However, if I add the+1
inprint(z+1, end="\t")
then the10
will not appear. Anyone know what I'm missing? I'd like to have the whole table show up. 
Non Negative Matrix Factorization: How to predict values for a new entry?
My current understanding:
I have tried reading a few papers and links regarding NMF. It all talks about how we can split a MxN matrix into MxR and RxN matrices(RQuestion:
I have a list of users(U) and some assignments(A) for each user. Now I split this matrix(UxA) using NMF. I get 2 Matrices UxR and RxA. How do I use these to predict what assignments(A') a new user(U') must have?Any help would be appreciated as I couldn't understand this after trying to search for the answer.
Side question and opinion based:
Also if anyone can tell me with their experience, how do they chose R, specially when the number of assignments are in the order of 50,000 or perhaps a hundred thousand. I have been trying these with the scikitlearn library 
What does the prime_division do?
rubydoc doesn't clearly explain what this method does. It states that a it returns a factorization of a number. And yet in their example they had:
Prime.prime_division(12) #=> [[2,2],[3,1]]
I don't understand how
prime_division(12) == [[2,2],[3,1]]
. Also when I try to call prime_division it doesn't work. I need a clear and thorough explanation.