#Any conditions I've missed?
106 messages · Page 1 of 1 (latest)
When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For more information use !howto ask.
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int a,b;
cout << "Enter a natural number: ";
cin>> a;
b = sqrt(a);
if(a==1) {cout<< "Not Prime";}
else if(a==2 |\| a==3 |\| a==5 |\| a==7) {cout<< "Prime";}
else if(a%2==0 |\| a%3==0 |\| a%5==0 |\| a%7==0 |\| a%b == 0) {cout<< "Not Prime";}
else {cout<<"Prime";}
return 0;
}
statistically speaking about everything
this is not the correct way to find a prime
where will it not work?
11
11 works
how about 143
143
@tame quiver
;compile ```cpp
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int a,b;
a=187;//cin>> a;
b = sqrt(a);
if(a==1) {cout<< "Not Prime";}
else if(a==2 || a==3 || a==5 || a==7) {cout<< "Prime";}
else if(a%2==0 || a%3==0 || a%5==0 || a%7==0 || a%b == 0) {cout<< "Not Prime";}
else {cout<<"Prime";}
return 0;
}```
Prime
187
so int b always rounds down if b is decimal?
I am quite certain
i put the sqrt there because of numbers like 169
Yes but it's kinda stupid, isn't it?
But say a number that is made by a multiplying 2 primes can be unique
But you need some fancier code for this
why? it deals with all the numbers that only have 3 factors: 1, itself, and its square root. And if b is a decimal then there's still no problem
yea i think this is the only problem
I kinda made a program to check if a number was unique and then i ran it for values under 1000 and there were 53 occurences where it didn't have the right answer;
This is the same as sayingif the number isn't a square it's prime
yea if that was the only condition i put in my code
else if(a%2==0 || a%3==0 || a%5==0 || a%7==0 || a%b == 0) {cout<< "Not Prime";}
I realized that
New attempt
if the number is 2 3 5 7 or alternatively if it doesn't have any of those as divisors and isn't perfect square, then its prime
yea, i've realised this generally works but its still wrong
GENERALLY WORKS?!
well at what point can you say generally
ok it doesn't matter if it works 95% of the time or 3% of the time
its still wrong, so how do i implement the condition?
Well what's the definition?
of primes?
yeah
a natural number that doesn't have any divisors other than 1 and itself?
no
small voice
it would be an infinite code then
itself
bzt bzt
eh?
Keep your mind churning
well not wrong, but for primes that is irrelevant
any number is always divisable by itself
Hint: loops exist
recursion ||probably not right||
well honestly there is no way to check primes without looping of some kind
Well integers are finite sized right ...
It wouldn't work because you would almost certainly need to generate that list with code which wood use a loop
This doesn't work

why not
promise it's correct
ah forgot the division by 0 and 1
need to remove those
nah those need to be tested 
The stupid part is that I am actually trying to do this
tbh made this as a bit of a joke
And why would that stop me?
this person means buisnuiss 
;cpp << 0x7fffffff
2147483647
Runtime error in iterm_3
On line 1 at position 1
log10(2147483647)
^
Failed to access variable log10
9.3 approx
it's generally easier to figure out if various numbers are not prime
like, except 2, any even number is not prime
except 5 iteself, any number divisible by 5 is not prime
this means that if the first digit (lowest order of magnitude) is 0, 2, 4, 5, 6, or 8 then the number is not prime
so you can rule out a lot of options just by looking at the first digit
or, said another way, only bother with the more expensive computation if the first digit is 1, 3, 7 or 9
Well i ended up running my program to generate a program that would work for every positivie integer and it is 1.6 gigs and vs Is really struggeling to open it
well
do you understand how prime numbers work? there are much more elegant solutions
I don't see how there could be anything more elegant than a predifined if statement
i meant something like a consteval sieve of primes approach
The problem isn't the speed of the program (A little bit) but the main problem is that thefile is way to large and vs crashes every time that try to open it and notepad does the same
268337161 is not a prime
268‚337‚161
And?
This question thread is being automatically closed. If your question is not answered feel free to bump the post or re-ask. Take a look at !howto ask for tips on improving your question.
