Python Prime Generator
Yes, I know there are a ton of these out there, and my question has probably already been answered somewhere I have not seen. However, I am still having difficulty and am unsure as to what to do.
maximum_range = int(input('How big do you want the range?'));
i = 3;
checkPrime = True;
while (i <= maximum_range):
y = 2;
print('i {}'.format(i))
print('y {}'.format(y))
halfI = int((i/2)+ 1)
checkPrime = False;
while (y <= halfI & checkPrime):
y += 1
if (i%y == 0):
print('{} is not prime because it is divisible by' .format(i))
checkPrime = False;
i += 2
if (checkPrime & y == halfI):
print('{}s is prime' .format(i))
checkPrime = True;
The problem is the nested iteration does not work, it reaches 'here 1', and that's it. I have also printed y and i- which print the correct values, yet it seems to not do anything.
Many thanks to those helping- please be patient, I much prefer Javascript.
python generator primes
add a comment |
Yes, I know there are a ton of these out there, and my question has probably already been answered somewhere I have not seen. However, I am still having difficulty and am unsure as to what to do.
maximum_range = int(input('How big do you want the range?'));
i = 3;
checkPrime = True;
while (i <= maximum_range):
y = 2;
print('i {}'.format(i))
print('y {}'.format(y))
halfI = int((i/2)+ 1)
checkPrime = False;
while (y <= halfI & checkPrime):
y += 1
if (i%y == 0):
print('{} is not prime because it is divisible by' .format(i))
checkPrime = False;
i += 2
if (checkPrime & y == halfI):
print('{}s is prime' .format(i))
checkPrime = True;
The problem is the nested iteration does not work, it reaches 'here 1', and that's it. I have also printed y and i- which print the correct values, yet it seems to not do anything.
Many thanks to those helping- please be patient, I much prefer Javascript.
python generator primes
2
You didn't say what the difficulty is, you haven't asked a question, you haven't described how it is unsatisfactory. Have you tried printing variables, comparison results, and other things at strategic locations to see what might be happening?
– wwii
Nov 12 '18 at 13:27
1
what exactly is the difficulty you are having? Does this code work? or you want it to produce something else?
– MEdwin
Nov 12 '18 at 13:31
Python does not require semicolons at the end of statements, Python coders reading that may find it annoying and to top it off you have scattered them around randomly on a subset of statements. That's a style comment, not your problem.
– wwii
Nov 12 '18 at 13:31
Welcome to SO, Please take the time to read How to Ask and the other links found on that page. As is your question is a little too broad. Focus on one way your code may be misbehaving and ask about that. If you find a solution to that question and there is still a problem, try to figure it out then ask a new question about that.
– wwii
Nov 12 '18 at 13:44
As written, your inner loop will never run - is that loop's comparison behaving the way you thought t would? ; it looks like you tried to optimize by only searching half the search space (withhalfI) maybe simplify and remove all that logic and see if it will work, testing with small values ofmaximum_range.
– wwii
Nov 12 '18 at 13:49
add a comment |
Yes, I know there are a ton of these out there, and my question has probably already been answered somewhere I have not seen. However, I am still having difficulty and am unsure as to what to do.
maximum_range = int(input('How big do you want the range?'));
i = 3;
checkPrime = True;
while (i <= maximum_range):
y = 2;
print('i {}'.format(i))
print('y {}'.format(y))
halfI = int((i/2)+ 1)
checkPrime = False;
while (y <= halfI & checkPrime):
y += 1
if (i%y == 0):
print('{} is not prime because it is divisible by' .format(i))
checkPrime = False;
i += 2
if (checkPrime & y == halfI):
print('{}s is prime' .format(i))
checkPrime = True;
The problem is the nested iteration does not work, it reaches 'here 1', and that's it. I have also printed y and i- which print the correct values, yet it seems to not do anything.
Many thanks to those helping- please be patient, I much prefer Javascript.
python generator primes
Yes, I know there are a ton of these out there, and my question has probably already been answered somewhere I have not seen. However, I am still having difficulty and am unsure as to what to do.
maximum_range = int(input('How big do you want the range?'));
i = 3;
checkPrime = True;
while (i <= maximum_range):
y = 2;
print('i {}'.format(i))
print('y {}'.format(y))
halfI = int((i/2)+ 1)
checkPrime = False;
while (y <= halfI & checkPrime):
y += 1
if (i%y == 0):
print('{} is not prime because it is divisible by' .format(i))
checkPrime = False;
i += 2
if (checkPrime & y == halfI):
print('{}s is prime' .format(i))
checkPrime = True;
The problem is the nested iteration does not work, it reaches 'here 1', and that's it. I have also printed y and i- which print the correct values, yet it seems to not do anything.
Many thanks to those helping- please be patient, I much prefer Javascript.
python generator primes
python generator primes
edited Nov 14 '18 at 19:03
Edd
asked Nov 12 '18 at 13:23
EddEdd
11
11
2
You didn't say what the difficulty is, you haven't asked a question, you haven't described how it is unsatisfactory. Have you tried printing variables, comparison results, and other things at strategic locations to see what might be happening?
– wwii
Nov 12 '18 at 13:27
1
what exactly is the difficulty you are having? Does this code work? or you want it to produce something else?
– MEdwin
Nov 12 '18 at 13:31
Python does not require semicolons at the end of statements, Python coders reading that may find it annoying and to top it off you have scattered them around randomly on a subset of statements. That's a style comment, not your problem.
– wwii
Nov 12 '18 at 13:31
Welcome to SO, Please take the time to read How to Ask and the other links found on that page. As is your question is a little too broad. Focus on one way your code may be misbehaving and ask about that. If you find a solution to that question and there is still a problem, try to figure it out then ask a new question about that.
– wwii
Nov 12 '18 at 13:44
As written, your inner loop will never run - is that loop's comparison behaving the way you thought t would? ; it looks like you tried to optimize by only searching half the search space (withhalfI) maybe simplify and remove all that logic and see if it will work, testing with small values ofmaximum_range.
– wwii
Nov 12 '18 at 13:49
add a comment |
2
You didn't say what the difficulty is, you haven't asked a question, you haven't described how it is unsatisfactory. Have you tried printing variables, comparison results, and other things at strategic locations to see what might be happening?
– wwii
Nov 12 '18 at 13:27
1
what exactly is the difficulty you are having? Does this code work? or you want it to produce something else?
– MEdwin
Nov 12 '18 at 13:31
Python does not require semicolons at the end of statements, Python coders reading that may find it annoying and to top it off you have scattered them around randomly on a subset of statements. That's a style comment, not your problem.
– wwii
Nov 12 '18 at 13:31
Welcome to SO, Please take the time to read How to Ask and the other links found on that page. As is your question is a little too broad. Focus on one way your code may be misbehaving and ask about that. If you find a solution to that question and there is still a problem, try to figure it out then ask a new question about that.
– wwii
Nov 12 '18 at 13:44
As written, your inner loop will never run - is that loop's comparison behaving the way you thought t would? ; it looks like you tried to optimize by only searching half the search space (withhalfI) maybe simplify and remove all that logic and see if it will work, testing with small values ofmaximum_range.
– wwii
Nov 12 '18 at 13:49
2
2
You didn't say what the difficulty is, you haven't asked a question, you haven't described how it is unsatisfactory. Have you tried printing variables, comparison results, and other things at strategic locations to see what might be happening?
– wwii
Nov 12 '18 at 13:27
You didn't say what the difficulty is, you haven't asked a question, you haven't described how it is unsatisfactory. Have you tried printing variables, comparison results, and other things at strategic locations to see what might be happening?
– wwii
Nov 12 '18 at 13:27
1
1
what exactly is the difficulty you are having? Does this code work? or you want it to produce something else?
– MEdwin
Nov 12 '18 at 13:31
what exactly is the difficulty you are having? Does this code work? or you want it to produce something else?
– MEdwin
Nov 12 '18 at 13:31
Python does not require semicolons at the end of statements, Python coders reading that may find it annoying and to top it off you have scattered them around randomly on a subset of statements. That's a style comment, not your problem.
– wwii
Nov 12 '18 at 13:31
Python does not require semicolons at the end of statements, Python coders reading that may find it annoying and to top it off you have scattered them around randomly on a subset of statements. That's a style comment, not your problem.
– wwii
Nov 12 '18 at 13:31
Welcome to SO, Please take the time to read How to Ask and the other links found on that page. As is your question is a little too broad. Focus on one way your code may be misbehaving and ask about that. If you find a solution to that question and there is still a problem, try to figure it out then ask a new question about that.
– wwii
Nov 12 '18 at 13:44
Welcome to SO, Please take the time to read How to Ask and the other links found on that page. As is your question is a little too broad. Focus on one way your code may be misbehaving and ask about that. If you find a solution to that question and there is still a problem, try to figure it out then ask a new question about that.
– wwii
Nov 12 '18 at 13:44
As written, your inner loop will never run - is that loop's comparison behaving the way you thought t would? ; it looks like you tried to optimize by only searching half the search space (with
halfI) maybe simplify and remove all that logic and see if it will work, testing with small values of maximum_range .– wwii
Nov 12 '18 at 13:49
As written, your inner loop will never run - is that loop's comparison behaving the way you thought t would? ; it looks like you tried to optimize by only searching half the search space (with
halfI) maybe simplify and remove all that logic and see if it will work, testing with small values of maximum_range .– wwii
Nov 12 '18 at 13:49
add a comment |
1 Answer
1
active
oldest
votes
You have missunderstand python's operators:
As pointed out here: https://docs.python.org/3/reference/expressions.html#binary-bitwise-operations
The operator & yields the bitwise AND of its arguments, which must be integers.
What you should use is the "and" logical operator.
Another thing that you should care is about operator precedence: https://docs.python.org/3/reference/expressions.html#operator-precedence
About the logic of your code, as long as you discover new prime numbers you can use them to divide (perform modulus operation) too much faster (fewer steps), because every number can decompose in a prime factors product. I dont give you an example, because i don't know excatly what do you want to achieve, if you give an example of what do you need I can make it.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53263133%2fpython-prime-generator%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You have missunderstand python's operators:
As pointed out here: https://docs.python.org/3/reference/expressions.html#binary-bitwise-operations
The operator & yields the bitwise AND of its arguments, which must be integers.
What you should use is the "and" logical operator.
Another thing that you should care is about operator precedence: https://docs.python.org/3/reference/expressions.html#operator-precedence
About the logic of your code, as long as you discover new prime numbers you can use them to divide (perform modulus operation) too much faster (fewer steps), because every number can decompose in a prime factors product. I dont give you an example, because i don't know excatly what do you want to achieve, if you give an example of what do you need I can make it.
add a comment |
You have missunderstand python's operators:
As pointed out here: https://docs.python.org/3/reference/expressions.html#binary-bitwise-operations
The operator & yields the bitwise AND of its arguments, which must be integers.
What you should use is the "and" logical operator.
Another thing that you should care is about operator precedence: https://docs.python.org/3/reference/expressions.html#operator-precedence
About the logic of your code, as long as you discover new prime numbers you can use them to divide (perform modulus operation) too much faster (fewer steps), because every number can decompose in a prime factors product. I dont give you an example, because i don't know excatly what do you want to achieve, if you give an example of what do you need I can make it.
add a comment |
You have missunderstand python's operators:
As pointed out here: https://docs.python.org/3/reference/expressions.html#binary-bitwise-operations
The operator & yields the bitwise AND of its arguments, which must be integers.
What you should use is the "and" logical operator.
Another thing that you should care is about operator precedence: https://docs.python.org/3/reference/expressions.html#operator-precedence
About the logic of your code, as long as you discover new prime numbers you can use them to divide (perform modulus operation) too much faster (fewer steps), because every number can decompose in a prime factors product. I dont give you an example, because i don't know excatly what do you want to achieve, if you give an example of what do you need I can make it.
You have missunderstand python's operators:
As pointed out here: https://docs.python.org/3/reference/expressions.html#binary-bitwise-operations
The operator & yields the bitwise AND of its arguments, which must be integers.
What you should use is the "and" logical operator.
Another thing that you should care is about operator precedence: https://docs.python.org/3/reference/expressions.html#operator-precedence
About the logic of your code, as long as you discover new prime numbers you can use them to divide (perform modulus operation) too much faster (fewer steps), because every number can decompose in a prime factors product. I dont give you an example, because i don't know excatly what do you want to achieve, if you give an example of what do you need I can make it.
answered Nov 14 '18 at 19:19
Juan Ignacio SánchezJuan Ignacio Sánchez
331112
331112
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53263133%2fpython-prime-generator%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
You didn't say what the difficulty is, you haven't asked a question, you haven't described how it is unsatisfactory. Have you tried printing variables, comparison results, and other things at strategic locations to see what might be happening?
– wwii
Nov 12 '18 at 13:27
1
what exactly is the difficulty you are having? Does this code work? or you want it to produce something else?
– MEdwin
Nov 12 '18 at 13:31
Python does not require semicolons at the end of statements, Python coders reading that may find it annoying and to top it off you have scattered them around randomly on a subset of statements. That's a style comment, not your problem.
– wwii
Nov 12 '18 at 13:31
Welcome to SO, Please take the time to read How to Ask and the other links found on that page. As is your question is a little too broad. Focus on one way your code may be misbehaving and ask about that. If you find a solution to that question and there is still a problem, try to figure it out then ask a new question about that.
– wwii
Nov 12 '18 at 13:44
As written, your inner loop will never run - is that loop's comparison behaving the way you thought t would? ; it looks like you tried to optimize by only searching half the search space (with
halfI) maybe simplify and remove all that logic and see if it will work, testing with small values ofmaximum_range.– wwii
Nov 12 '18 at 13:49