On Saturday, 5 September 2015 at 18:57:52 UTC, deed wrote:
On Saturday, 5 September 2015 at 17:31:39 UTC, Namal wrote:
Yeah, I have have been trying this example from wiki books

https://en.wikibooks.org/wiki/Learning_D_With_Project_Euler

It is not even compiling.

What exactly is not compiling?

the last codesample on the bottom. I think because of the old D? Index for the last array.length-1 is now $-1. But also I get

Error: undefined identifier 'file'

for the read line. But even when I fixed those errors the strings I got were with those quotation marks and backslashes. However, with your help I could solve it now.

I moved to the next problem and wrote the program for it

import std.stdio, std.algorithm, std.array;

bool abundant(int n){
        
        int[] a;
        
        foreach(i;1..n)
                if(!(n%i))
                        a~=i;
        auto sum = reduce!((a,b)=>a+b)(0,a);
        
        return sum>n;
}



void main(){
        
        long sum;
        int[] arr;
        int[28123] mark;
        
        foreach(i;1..28124)
                if(abundant(i))
                        arr~=i;
        
        foreach(i;arr)
        foreach(j;arr){
                
                if(i+j>28123)
                        break;
                mark[i+j-1] = 1;
        }
        for(auto i = 0;i<mark.length;++i)
                if(!mark[i])
                        sum+=i+1;
        writeln(sum);
}

How can I generate the array arr the functional way with reduce and the function abundant as filter. Tried it on my own but failed. Also, if I use reduce on an array, like I did for the sum, what does the zero mean in the range?



Reply via email to