Skip to main content
Announcements
Qlik Connect 2024! Seize endless possibilities! LEARN MORE
cancel
Showing results for 
Search instead for 
Did you mean: 
brocklewis
Contributor II
Contributor II

Solving Euler's Problems with QlikView Script Editor

Hello everyone & Happy Holidays,

I am relatively new to QlikView development, and have been told that a good exercise for all coding languages (even simple ones) is to solve some of Euler's problems. 

For those unfamiliar with Project Euler, here is a link to the list of problems: https://projecteuler.net/archives

The first problem is quite simple: to find the sum of all of the numbers with multiples of 3 or 5 that are below 1000. 

For my solution, I wanted my first step to generate a table consisting of the numbers 1 through 999, along with a flag that returned "1" if that number was divisible by either 3 or 5, and a "0" otherwise. But for some reason, this load statement returns nothing. No fields are even generated. 

Set a=1;
Load
a + IterNo() As Number
,If(Mod(a + IterNo(),3)=0 OR Mod(a + IterNo(),5)=0,1,0) As DivisibleFlag

While a + IterNo() < 1000;

The second step in my solution would be to create a text box in the .qvw which simply had the following expression:  =Sum({$<DivisibleFlag = 1>Number}

It seems like I'm making some basic mistake with syntax, but I haven't been able to find where I'm going wrong so far. Any help will be appreciated.

 

EDIT: Typo in link to Project Euler.

1 Solution

Accepted Solutions
skamath1
Creator III
Creator III

Try the below script

 

LOAD
a + IterNo() As Number
,If(Mod(a + IterNo(),3)=0 OR Mod(a + IterNo(),5)=0,1,0) As DivisibleFlag
While a + IterNo() - 1 < e;

 

LOAD * INLINE
[a,e
1,1000
];

View solution in original post

3 Replies
skamath1
Creator III
Creator III

Try the below script

 

LOAD
a + IterNo() As Number
,If(Mod(a + IterNo(),3)=0 OR Mod(a + IterNo(),5)=0,1,0) As DivisibleFlag
While a + IterNo() - 1 < e;

 

LOAD * INLINE
[a,e
1,1000
];

brocklewis
Contributor II
Contributor II
Author

Thanks! This works. Can you possibly explain why your script works, and mine does not? The logic is almost identical, you're just drawing on your initial a value and your final e value by referencing an in-line table--and I don't see why that would work, but using set a = 1, would not. 

MarcoWedel

Hi,

another solution could be:

LOAD RecNo() as Number,
If(Mod(RecNo(),3) and Mod(RecNo(),5),0,1) as DivisibleFlag
AutoGenerate 999;

and an expression like

Sum({$<DivisibleFlag={1}>} Number)

hope this helps

regards

Marco