Quantum algorithm for finding periodicities in the spectrum of a black-box Hamiltonian or unitary transformation