with(numtheory); [seq(Phi_pos_terms(j, 2), j=0..104)];
inv_p_mod_q := (p, q) -> op(2, op(1, msolve(p*x=1, q))); # Find's p's inverse modulo q.
dilate := proc(nn, x, e) local n, i, s; n := nn; i := 0; s := 0; while(n > 0) do s := s + (((x^e)^i)*(n mod x)); n := floor(n/x); i := i+1; od; RETURN(s); end;
Phi_pos_terms := proc(n, x) local a, m, p, q, e, f, r, s; if(n < 2) then RETURN(x); fi; a := op(2, ifactors(n)); m := nops(a); p := a[1][1]; e := a[1][2]; if(1 = m) then RETURN(((x^(p^e))-1)/((x^(p^(e-1)))-1)); fi; if(2 = m) then q := a[2][1]; f := a[2][2]; r := inv_p_mod_q(p, q)-1; s := inv_p_mod_q(q, p)-1; RETURN( (`if`(0=s, 1, (((x^((s+1)*((q^f)*(p^(e-1)))))-1)/((x^((q^f)*(p^(e-1))))-1)))) * (`if`(0=r, 1, (((x^((r+1)*((p^e)*(q^(f-1)))))-1)/((x^((p^e)*(q^(f-1))))-1)))) ); fi; if((3 = m) and (2 = p)) then if(1 = e) then RETURN(every_other_pos(Phi_pos_terms(n/2, x), x, 0)+every_other_pos(Phi_neg_terms(n/2, x), x, 1)); else RETURN(dilate(Phi_pos_terms((n/(2^(e-1))), x), x, 2^(e-1))); fi; else printf(`Cannot handle argument %a with three or more distinct odd prime factors!\n`, n); RETURN(0); fi; end;