Advertisement
zvoulgaris

Untitled

Jan 29th, 2020
496
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.98 KB | None | 0 0
  1. # Non-Abundant Sums drill
  2.  
  3. function ProperDivisors(n::Int64)
  4. q = min(1000, n)
  5. z = Array{Int64}(undef, q)
  6. z[1] = 1
  7. c = 1
  8.  
  9. for d = 2:round(Int64, sqrt(n))
  10. w, r = divrem(n, d)
  11.  
  12. if r == 0
  13. c += 1
  14. z[c] = d
  15. c += 1
  16. z[c] = w
  17. end
  18. end
  19.  
  20. return sort(z[1:c])
  21. end
  22.  
  23. function IsAbundant(n::Int64)
  24. z = ProperDivisors(n)
  25. return (sum(z) > n)
  26. end
  27.  
  28. function CanBeWrittenAsSumOfAN(n::Int64)
  29. if n > 28123; return true; end
  30. n2 = div(n, 2)
  31.  
  32. for x = 12:n2
  33. if IsAbundant(x)
  34. y = n - x
  35. if IsAbundant(y); return true, (x, y); end
  36. end
  37. end
  38.  
  39. return false
  40. end
  41.  
  42. function main(N::Int64 = 28123)
  43. for n = 1:26
  44. if !CanBeWrittenAsSumOfAN(n)[1]; print(n, " "); end
  45. end
  46.  
  47. for n = 27:2:N # no point checking for even numbers any more
  48. if !CanBeWrittenAsSumOfAN(n)[1]; print(n, " "); end
  49. end
  50. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement