-1

I have written a code(in python) that produces a number and checks if the number is prime or not, if it is a prime it will print it. But my code keeps producing numbers and printing them, can you give feedback on what is wrong in my approach?

val = 10
for i in range (2, (val+1)//2):
    while (val+1) % i != 0 :
        print(val + 1)
        val = val *10

I want to check if any number ( that is multiple of ten added by one) is considered a prime number or not.

Anttarm
  • 5
  • 2
  • Does this answer your question? [Infinite range in my python prime finder?](https://stackoverflow.com/questions/20446006/infinite-range-in-my-python-prime-finder) – Bruno Jan 03 '20 at 19:45
  • no that doesn't answer my question. – Anttarm Jan 03 '20 at 19:54
  • I don't want to find primes numbers, I want to check if my numbers are prime or not. – Anttarm Jan 03 '20 at 19:56
  • This is more of an mathematical question, do you actually need this or are you doing some kind of school assigment, this is a really broad discussion https://stackoverflow.com/questions/4114167/checking-if-a-number-is-a-prime-number-in-python/27946768, your code is actually an infinite loop since val = val *10 you never reach an ending – Bruno Jan 03 '20 at 20:00
  • No I actually need an answer, and yes it will be an infinite loop if my assumption is correct, and my assumption is {any number ( that is multiple of ten added by one) is considered a prime number or not}. – Anttarm Jan 03 '20 at 20:17
  • So I don't need to know anything about infinite loops, because my goal is to prove that general rule to find prime numbers, I know that my assumption is somehow wrong, but I want to know why. – Anttarm Jan 03 '20 at 20:19
  • And I am asking a similar question in other maths websites, I am asking here to know the actual code to test my assumption – Anttarm Jan 03 '20 at 20:21
  • Maybe you can stop at 121? – Bruno Jan 03 '20 at 20:22
  • I can't produce 121 since it is not a number that is multiple of ten added by one. like 101, 10001,100001,......etc. and my code will stop if it detected a number that is not a prime. – Anttarm Jan 03 '20 at 20:32

2 Answers2

0

A prime is a number x that is only dividable by x and 1. So you should start to iterate through all numbers between 2 and x-1 and look if the modulus is ever equal to zero.

val = 10
for i in range (2, val-1):
    if val%i == 0:
        print(i)
        break

You could leave the break if you want to see all the numbers your variable is dividable.

herrwolken
  • 389
  • 1
  • 4
  • 12
  • Thank for your answer, but this not what I am attempting to try. I want to to check if any number ( that is multiple of ten added by one) is consdered a prime number or not – Anttarm Jan 03 '20 at 19:52
0

Ok this was strangly worded but I got the gist of it, I think, first we need a function to detect prime numbers

import math

def is_prime(n):
    if n == 2:
        return True
    if n % 2 == 0 or n <= 1:
        return False

    sqr = int(math.sqrt(n)) + 1

    for divisor in range(3, sqr, 2):
        if n % divisor == 0:
            return False
    return True

Then if I got your idea right you want to test if a number is prime following this function x2 = x1 *10 + 1

So that gives us this

val = 10
while val <10000:
  if is_prime(val + 1) == False:
    print(val+1)
    break

  val = val * 10

Which will not go on forever, and actually breaks at the third loop at 1000(1001).

Bruno
  • 4,109
  • 1
  • 9
  • 27
  • Also if you want to get fancy look into https://rosettacode.org/wiki/Prime_decomposition#Python and print the result from decompose – Bruno Jan 03 '20 at 20:39