0

I am trying to figure out this question.

Write a procedure, primeset, whose input is a positive integer n and whose output is the set of all primes p such that p divides n.

I have tried the following so far. This is using maple.

primeset:=proc

 # Determine if n is divisble by p:

          local p;
          for p from 1 to n do
           if isprime(p) then # check divisibility by primes
            if modp(n,p) = 0 then # check if divided by prime
              return false;
             end if;
            end if;
          end do;
          return true;
          end proc;
          # VARIABLES:
          # INPUT:
          #  n is a (positive) integer
          # LOCAL:
          #  p is a (positive) integer.
          # OUTPUT:
          #  output is the set of all primes p such that p divides n.

My ouput was this:

primeset := proc (n) local p; for p to n do if isprime(p) then if modp(n, p) = 0 then return false end if end if end do; return true end proc.

I tried to run it on some numbers such 2,4,and 10, and all I got was false, false, false.

If anyone can offer some suggestions then that would be great.

Julius Vainora
  • 47,421
  • 9
  • 90
  • 102
Jamde Jam
  • 11
  • 1
  • 6

2 Answers2

1

Here are some hints:

The number n is the product of a list of prime factors.

Search for the smallest factor p >= 2.

The product of the remaining factors is n / p, so repeat, searching for the smallest prime factor of n / p, etc.

Remember that a factor may be repeated, so the next factor you find will be >= the last.

And finally, the largest factor of a number cannot exceed the square root of that number.

MRAB
  • 20,356
  • 6
  • 40
  • 33
1

Maple has built-in factorisation commands like ifactor(n), which output a list of prime factors, which you can use to make your list. Approach suggested by @MRAB is also very common, iterating over all possible prime factors and see if n contains it as a factor.

gotnull
  • 26,454
  • 22
  • 137
  • 203
Origin
  • 2,009
  • 5
  • 19
  • 19