I agree that there's is surely enough written here to see the pattern now. I do wonder what the homework is for (school?/uni?/subject?) though because it's not really about programming at the end of the day (Edit: that was a stupid thing for me to say; of course it's programming because problem solving is what programming is). After replacing my prime sieve with trial division (there's so few primes involved I dunno why I bothered with a sieve in the first place) my entire program is 20 lines long :/