|
Post by tsh73 on Feb 8, 2022 3:47:22 GMT -5
It is high-school test problem my kid got yesterday I jumped in - but after getting something working... I Googled and it happened that I solved quite anoter task (Doh).
So here is it (Google-translated text, original is in Russian)
Could you solve it without Google?
|
|
bplus
Full Member
Posts: 127
|
Post by bplus on Feb 17, 2022 0:13:14 GMT -5
' What kind of number has exactly 3 divisors???
row = 1 For i = 2 To 2500 scan Locate 1, row: Print i;" "; ' <<< progress report OMG! This is going to take far too long for 123456789+ numbers test = ThreeDivisors(i) If test Then Locate 1, row For j = 2 To i / 2 'show divisors scan If i Mod j = 0 Then 'j divides i Print " ";j; End If Next Print " divides "; i row = row + 1 End If Next Locate 1, row +1 Print "End of run."
Function ThreeDivisors (n) For i = 2 To n / 2 ' count divisors scan If n Mod i = 0 Then 'i divides n count = count + 1: lastDivisor = i If count > 3 Then Exit Function ' too many divisors End If Next If count = 3 Then ThreeDivisors = lastDivisor End Function
p, p^2, p^3 divides p^4 where p is prime.
OK so what integers to power of 4 are between 123456789 and 223456789
' what is the first integer ^ 4 >= 123456789 i = 2 While i ^ 4 < 123456789 i = i + 1 Wend a = i ^ 4 Print i, a
' what is the last integer i st i ^ 4 <= 223456789 While i ^ 4 <= 223456789 i = i + 1 Wend 'Ok backup i by 1 a = (i - 1) ^ 4 Print i - 1, a
OK so here is the solution:
For n = 106 To 122 ' n^4 > 123456789 and n^4 < 223456789 'if n is prime print the divisors Prime = -1 For j = 2 To Sqr(n) If n Mod j = 0 Then Prime = 0: Exit For Next If Prime Then Print n; ", "; n ^ 2; ", "; n ^ 3; " divides "; n ^ 4 Next Print
|
|
|
Post by tsh73 on Feb 17, 2022 2:59:38 GMT -5
Yes that's it. Interesting that if you think before, you could solve it with calculator. But if you don't - 100 000 000 numbers is very big chank to test... (and if you do not comprehend the task you could get to know some useless things. Like, there are some 19 000 000 numbers in that region having exactly 3 primes in factorisation. Or using // instead if / speeds up Python. Or using Python 2 instead of Python 3 speeds it up about 2x. Or using PyPy you could indeed check all these numbers overnight. But it does not get task solved. )
|
|