#Any conditions I've missed?

106 messages · Page 1 of 1 (latest)

tame quiver
#

The purpose is to test for prime numbers. Are there any conditions that should be accounted for?

thick perchBOT
#

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.

tame quiver
#

#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;

}

modern crown
#

statistically speaking about everything

#

this is not the correct way to find a prime

tame quiver
#

where will it not work?

potent pumice
tame quiver
#

11 works

potent pumice
#

how about 143

quiet shoreBOT
#

143

potent pumice
#

@tame quiver

tame quiver
#

for 143 it says not prime

#

but i've realised my mistake tho

potent pumice
#

;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;

}```

lament lanternBOT
#
Program Output
Prime
tame quiver
#

it doesn't work with 713

#

ok i think i need a complexer code

potent pumice
#

I figured out why 144 worked

#

*143

#

it's because sqrt rounded down

quiet shoreBOT
#

187

tame quiver
potent pumice
#

I am quite certain

tame quiver
#

i put the sqrt there because of numbers like 169

potent pumice
#

Yes but it's kinda stupid, isn't it?

modern crown
#

But you need some fancier code for this

tame quiver
tame quiver
potent pumice
#

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;

potent pumice
tame quiver
#

yea if that was the only condition i put in my code

potent pumice
#

Ok then

#

If the number is 2 3 5 7 or it's a square it's prime

tame quiver
#

else if(a%2==0 || a%3==0 || a%5==0 || a%7==0 || a%b == 0) {cout<< "Not Prime";}

potent pumice
#

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

tame quiver
#

yea, i've realised this generally works but its still wrong

potent pumice
#

GENERALLY WORKS?!

tame quiver
#

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?

potent pumice
#

Well what's the definition?

tame quiver
#

of primes?

modern crown
#

yeah

tame quiver
#

a natural number that doesn't have any divisors other than 1 and itself?

modern crown
#

yup :D

#

and are you checking every possible devisor? :D

tame quiver
#

no

potent pumice
#

small voice

tame quiver
#

it would be an infinite code then

modern crown
#

no ;)

#

whats the biggest a divisor could ever be?

tame quiver
#

itself

modern crown
#

bzt bzt

tame quiver
#

eh?

potent pumice
#

Keep your mind churning

modern crown
#

well not wrong, but for primes that is irrelevant

#

any number is always divisable by itself

potent pumice
#

Hint: loops exist

tame quiver
#

o

#

i haven't covered that yet

potent pumice
#

recursion ||probably not right||

modern crown
#

well honestly there is no way to check primes without looping of some kind

potent pumice
#

Well integers are finite sized right ...

modern crown
#

I know where this is going :D

potent pumice
modern crown
#

only works for uint16_t though

potent pumice
#

This doesn't work

modern crown
#

why not

#

promise it's correct

#

ah forgot the division by 0 and 1

#

need to remove those

potent pumice
#

and 4

#

and 6

#

and 8

#

and 10

#

and 12

modern crown
#

nah those need to be tested OMEGALUL

potent pumice
#

The stupid part is that I am actually trying to do this

modern crown
#

tbh made this as a bit of a joke

potent pumice
#

And why would that stop me?

modern crown
#

this person means buisnuiss OMEGALUL

potent pumice
#

;cpp << 0x7fffffff

lament lanternBOT
#
Program Output
2147483647
quiet shoreBOT
#
Runtime error in iterm_3
On line 1 at position 1

log10(2147483647)
^
Failed to access variable log10
potent pumice
#

9.3 approx

jolly moss
#

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

potent pumice
#

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

random nimbus
#

well

#

do you understand how prime numbers work? there are much more elegant solutions

potent pumice
#

I don't see how there could be anything more elegant than a predifined if statement

random nimbus
#

i meant something like a consteval sieve of primes approach

potent pumice
#

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

rigid forge
#

268337161 is not a prime

quiet shoreBOT
#

268‚337‚161

potent pumice
#

And?

quiet shoreBOT
thick perchBOT
#

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.