swift - Difference between 'for index' and traditional 'for loop' -
i'm new swift , pretty new programming. i'm not sure if it's because it's 2:34 can me identify difference between these 2 loops?
the first 1 resulting in values want yet uses (as swift documentation explains) 'traditional c loop' 1 after using seems swift preferred 'for index' loop (which clearer me), problem loop returns every number rather meeting conditional.
func findlargestprimefactor(number: int) { var = 2; < number; += { if number/i % 1 > 0 { } else { print(i); } } } findlargestprimefactor(13195);
below returning every number 13195
func findlargestprimefactor(number: int) { in 2...number { if number/i % 1 > 0 { } else { print(i); } } } findlargestprimefactor(13195);
i've realised how stupid mistakes were, first of have no idea why doing += i, didn't make sense @ all.
also, conditional never being met because declared int when needed float because care remainders!
edit: i've updated code yet again (taken everyones feedback consideration), feel bit better , continue working on solution.
thanks help!
updated code
func findlargestprimefactor(number: int) { in 2...number { var n = float(number) var count = float(i) if n/count % 1 > 0 { print("not divisible") } else { print(i) } } } findlargestprimefactor(13195);
Comments
Post a Comment