# How to generate number which exactly has seven divisors?

For a homework assignment, I need the logic to find a series of numbers from 1 to 1000 which have exactly seven divisors.

(Ideally, the code could be easily modified to generate prime numbers.)

-
One starting point to consider would be that to get an odd number of divisors, it must be a perfect square. – Jerry Coffin Mar 13 '11 at 15:41
Does 1 count as a divisor? – Gabe Mar 13 '11 at 17:57
@Gabe: from what I know it is always counted unless explicitly excluded. – liori Mar 13 '11 at 20:02

You need loop (for loop) from `for n = 1 to 1000` and inside this loop another loop `for m = 1 to n` inside this loop test if `n/m = integer (no remainder)` and if it is increment the div counter. At the end of second loop check if div counter is 7 if it is write the number on the screen.

EDIT: for prime numbers the div counter must be 2!

-
+1 because it also can easily be modified to generate prime numbers – anatolyg Mar 13 '11 at 18:11
It is horribly inefficient. – liori Mar 13 '11 at 20:02
@liori: Just a simple brute-force algorithem. It is possible to improve it, but why for only 1000 numbers? – GJ. Mar 13 '11 at 20:34
For 1000 numbers you should notice that 1000^(1/6) is just little more than 3. This means the only numbers less than 1000 that have 7 divisors can be 2^6 and 3^6. How much time would you need to type your algorithm? – liori Mar 14 '11 at 23:21
@liori: Agree, but this is homework and he need logic which can be easily modified to generate prime numbers. So? – GJ. Mar 15 '11 at 10:05

Take a prime number `p`. Calculate `p^6`. Its only divisors will be: `1`, `p`, `p^2`, `p^3`, ..., `p^6`.

-
int sevendivis(){ return 2*2*2*2*2*2; } – Jeffrey Greenham Mar 15 '11 at 19:21

A number with the factorisation

``````n = product(p_i ^ k_i)
``````

will have

``````d = product(k_i + 1)
``````

divisors (see divisor function in Wikipedia). This shows `n` may only have one prime factor, and this prime factor must be raised to the power of 6. So take the sixth power of an arbitrary prime number.

-

The Logic would be that the number would be both perfect Square and Perfect cube.

You must be Knowing that a number which has prime factor form as N=N1^a * N2^b; Where N1 and N2 are prime numbers has a*b factors or divisors.

Thus for 7 factors number must be of the form N=a^6 where a is a prime number .

for e.g 2^6 (64) , 3^6 (729).

EDIT: With this logic it would be quite easier to generate numbers quicly .You can easily generate perfect squares and perfect cube <1000.And check both the lists for common numbers.

-
{"found_math": false, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0}