Finding the LCM of a range of numbers

前端 未结 14 735
时光说笑
时光说笑 2020-12-08 05:03

I read an interesting DailyWTF post today, \"Out of All The Possible Answers...\" and it interested me enough to dig up the original forum post where it was submitted. This

相关标签:
14条回答
  • 2020-12-08 06:01

    An algorithm in Haskell. This is the language I think in nowadays for algorithmic thinking. This might seem strange, complicated, and uninviting -- welcome to Haskell!

    primes :: (Integral a) => [a]
    --implementation of primes is to be left for another day.
    
    primeFactors :: (Integral a) => a -> [a]
    primeFactors n = go n primes where
        go n ps@(p : pt) =
            if q < 1 then [] else
            if r == 0 then p : go q ps else
            go n pt
            where (q, r) = quotRem n p
    
    multiFactors :: (Integral a) => a -> [(a, Int)]
    multiFactors n = [ (head xs, length xs) | xs <- group $ primeFactors $ n ]
    
    multiProduct :: (Integral a) => [(a, Int)] -> a
    multiProduct xs = product $ map (uncurry (^)) $ xs
    
    mergeFactorsPairwise [] bs = bs
    mergeFactorsPairwise as [] = as
    mergeFactorsPairwise a@((an, am) : _) b@((bn, bm) : _) =
        case compare an bn of
            LT -> (head a) : mergeFactorsPairwise (tail a) b
            GT -> (head b) : mergeFactorsPairwise a (tail b)
            EQ -> (an, max am bm) : mergeFactorsPairwise (tail a) (tail b)
    
    wideLCM :: (Integral a) => [a] -> a
    wideLCM nums = multiProduct $ foldl mergeFactorsPairwise [] $ map multiFactors $ nums
    
    0 讨论(0)
  • 2020-12-08 06:01

    Here is the solution using C Lang

    #include<stdio.h>
        int main(){
        int a,b,lcm=1,small,gcd=1,done=0,i,j,large=1,div=0;
        printf("Enter range\n");
        printf("From:");
        scanf("%d",&a);
        printf("To:");
        scanf("%d",&b);
        int n=b-a+1;
        int num[30];
        for(i=0;i<n;i++){
            num[i]=a+i;
        }
        //Finds LCM
        while(!done){
            for(i=0;i<n;i++){
                if(num[i]==1){
                    done=1;continue;
                }
                done=0;
                break;
            }
            if(done){
                continue;
            }
            done=0;
            large=1;
            for(i=0;i<n;i++){
                if(num[i]>large){
                    large=num[i];
                }
            }
            div=0;
            for(i=2;i<=large;i++){
                for(j=0;j<n;j++){
                    if(num[j]%i==0){
                        num[j]/=i;div=1;
                    }
                    continue;
                }
                if(div){
                    lcm*=i;div=0;break;
                }
            }
        }
        done=0;
        //Finds GCD
        while(!done){
            small=num[0];
            for(i=0;i<n;i++){
                if(num[i]<small){
                    small=num[i];
                }
            }
            div=0;
            for(i=2;i<=small;i++){
                for(j=0;j<n;j++){
                    if(num[j]%i==0){
                        div=1;continue;
                    }
                    div=0;break;
                }
                if(div){
                    for(j=0;j<n;j++){
                        num[j]/=i;
                    }
                    gcd*=i;div=0;break;
                }
            }
            if(i==small+1){
                done=1;
            }
        }
        printf("LCM = %d\n",lcm);
        printf("GCD = %d\n",gcd);
        return 0;
    }
    
    0 讨论(0)
提交回复
热议问题