# code to input a number: # nnst = input("Give me an integer here: ") # nn = int(nnst) nn = 186799 # this assigns nn to be an integer. print("Do you think "+str(nn)+" is prime?") # these are strings; print displays it as output. for j in range(2,nn): # range(2,nn) means the integers 2,3,4,...,nn-1 # each time through the loop, j becomes 2, then 3, then 4, ... # and the loop is run nn-2 times. if nn/j==int(nn/j): # need two = signs if you are checking if an equation is true are not. # a==b means true if a=b, false if a!=b. '!=' means not equal. #int(x) = floor(x) = x rounded down to next integer (or if it's an integer then it # is itself). print('It turns out that '+str(nn)+' is not prime') break #this makes the for loop stop. "break" cuts you out of any loop #print(j) if j==nn-1 and nn/j!=int(nn/j): # if it went all the way through the for loop # and the last nn/j is not an integer. print("Guess what? The number "+str(nn)+" is prime.") https://sagecell.sagemath.org/?z=eJx9U9GO2jAQfEfiH5bkIUSXy-m4U9ujiu6l0qnv_QEnLBAINrWdAn_fWRtyQaqalxAzOzs7O06pMSsmb6jVx96TIt0farbL6SQlrZ2nKv4zTz7aP0wHJqVx4nnDlrZseUlJHsEB6udShZPw_fzty9e3N0rJb1tHyrl2o51A0bAeU5XTydFKdfLD0MX0UqD3lDw4b8GYPyQEAkAO_J7kgZAdCCwTEK3euO8UCGjVumOnLo5aTOPI9B7qQb82lnZoR1bpDc8XBViXIBp9YjoFeaC-yXK0KF6K16IsSwAen2k6ITwpsWq25KEGaGv6zTZUdcYcC3SpuTEHlmI51vRyfb8WBKaBROnVUCbj2V7Dm8dFIHZlhLVrnD3tqip6-7QLqjUzak8GHkdPARPbxJBmy80elsgZHObfvfKt0dLB2z6apo2_8l-lVFV9G18wUlrVBa1V5-LXrKpLymZVdoWBIVB3Yx7ReM4hao2ZbPx5JhikV9C7Mqewec1nP2Rojr2Av_WZG0cr-IUVjkW2slTH3Tof9YyxyX568r3VYeEoVp6yITuZVIrekJ8s_yyuLas9pSGdB7XnuHyJStiJ8-ZYUhJQCTW9d8FkaWHE20uAIf1RxC4n6MUw2FZIiyxYVja7X14Yl06MtKquCy1P6nKXpEHCLSv_e4YcKdxX6XKb9-5-fZqVfPTsHJ3g0jv9QmG88_-8bWWS_wUNP0Eh&lang=python&interacts=eJyLjgUAARUAuQ== # code to input a number: # nnst = input("Give me an integer here: ") # nn = int(nnst) nn = 186711 # this assigns nn to be an integer. print("Do you think "+str(nn)+" is prime?") # these are strings; print displays it as output. lastj = int(sqrt(nn)); # only need to check divisibility of numbers up to the square root of nn flagIfPrime = True for j in range(2,lastj+1): # range(2,nn) means the integers 2,3,4,...,lastj-1 # each time through the loop, j becomes 2, then 3, then 4, ... # and the loop is run lastj-2 times. if nn%j==0: # need two = signs if you are checking if an equation is true are not. # a==b means True if a=b, False if a!=b. '!=' means not equal. # nn%j means nn mod j = remainder when nn is divided by j. print('It turns out that '+str(nn)+' is not prime') flagIfPrime = False break #this makes the for loop stop. "break" cuts you out of any loop #print(j) if flagIfPrime: # if it went all the way through the for loop # and the last nn/j is not an integer. print("Guess what? The number "+str(nn)+" is prime.") https://sagecell.sagemath.org/?z=eJx9U9tum0AQfbfkf5hgVRiZ0tqJ2ioRykvVKG95yA8sMIbFsEv2Eou_78yCb1JVXhDDmXPOnpldQakrBKdBqsE7EKB8X6B5XC5WoJR1kE9_1tGL_EToEYSiisMaDTRo8BGiZAIHqFtzF1XC9_bXj5_bLazANdKCsFbWyjKUBItrqmy5GAx3R781jNpzgzpAtLHOEGOyiYAICNLjc5QEQrREYBAIIVVtnyAQQCXt0InRgqTTWNDekXui74R17WzRfhj2mSRPxKRVN4JCrNhU2WB5II5PaWUhO-lG0Ps5Ewt-YAxJg_3wrG20dgGglot9J-rX_RtbJJl345Fq2kBLkmCEqnG9S4OLzTZ5JOFTjYxQroKCYeY5EAu79D59SLMsm5q-bmG5AHpWgKJswLGOa4z2dRMaO62HlNQKLHWP3M9lBffz-yEFIjuTCFWd2zhb4xVMQrvAbbMJKfl0X9o8_86ep5yOmk44zZJ-87g4jJAdjYJrNFmkiJzUiskdpREwSruZd3aR58V8eE4stOZFCn9EZ6evu7zIIL7L4xlGDIG6u-FhiyeAgl5XwLM22AupKtrUIyegghcebkWnKEZorzim9YtfHThvVFgcykc4iM87GHM764c9jJNL8-3sg_nLz8KgOMAqXIFeHHCaM69GyN46PWQQBVQEpXc2JMr6moMcA4yu2OSwTYBmSMlcafJkqEIbf0S6AqLrgsRRjDcbcpY87cD_nvN-0E5Qct_a0-FvLu0luejFo7UUtHDP8E6N06X55xXOouQv4P1gCA==&lang=python&interacts=eJyLjgUAARUAuQ==